CadQuery / cadquery

A python parametric CAD scripting framework based on OCCT
https://cadquery.readthedocs.io
Other
3.2k stars 290 forks source link

dimension inputs for box requires separate variables rather than a vector #53

Open dcowden opened 5 years ago

dcowden commented 5 years ago

Issue by burdickjp Friday Apr 15, 2016 at 13:07 GMT Originally opened as https://github.com/dcowden/cadquery/issues/141


I would like to feed box a vector for x,y,z dimensions rather than having to explicitly state x,y,z variables. Something like: dim = [2, 1, 1] result = cadquery.Workplane("XY").box(dim)

rather than having to do this: result = cadquery.Workplane("XY").box(dim[0], dim[1], dim[2])

dcowden commented 5 years ago

Comment by dcowden Friday Apr 15, 2016 at 14:49 GMT


hmm that's interesting. Very do-able. Are you adventurous enough to make the change and do a pull request? We'd happily accept that.

Otherwise, we've a completely new version ( CQ 2.0) on the drawing board, and we'll integrate this into that effort.

On Fri, Apr 15, 2016 at 9:07 AM, Jeffrey Burdick notifications@github.com wrote:

I would like to feed box a vector for x,y,z dimensions rather than having to explicitly state x,y,z variables. Something like: dim = [2, 1, 1] result = cadquery.Workplane("XY").box(dim)

rather than having to do this: result = cadquery.Workplane("XY").box(dim[0], dim[1], dim[2])

— You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub https://github.com/dcowden/cadquery/issues/141

dcowden commented 5 years ago

Comment by burdickjp Saturday Apr 16, 2016 at 01:34 GMT


I tried a pull request, but I'm not very good at these things :(

CQ 2.0 sounds exciting. I'll be honest, I don't find CQ as it currently sits very human legible in comparison to OpenSCAD. I hope CQ 2.0 is a bit easier to read and write.

I HATE that OpenSCAD can't make STEP files. How am I supposed to CNC my parts if round features aren't actually round?!

dcowden commented 5 years ago

Comment by dcowden Saturday Apr 16, 2016 at 01:41 GMT


yes, i see the PR, thanks! I wont be able to merge it tonight but i'll get to it soon.

The 2.0 re-write is really about two things:

  1. removing the FreeCAD dependency and thus getting ability to get away from its constraints
  2. adding a proper feature tree, so that you can filter and select features base on the operation that added them

Unfortunately, it will very likely not change the syntax of scripts themselves, since that's not the plan.

I'd love to hear what things you find are difficult about cQ syntax. I personally think it is MUCH better than OpenSCAD, due primarily to the fact that it is actually python, vs some custom language, and due to the ability to chain methods together. Of course i'm biased obviously. OpenSCAD scripts are very long and hard to write because of its CSG heratage. cQ scripts are nearly always much shorter than their equivalent OS scripts.

All that said, i'd be happy to integrate suggestions to make it better, if they make sense. I liked your suggestion to allow an array instead of passing the dimensions themselves. Thanks for contributing!

On Fri, Apr 15, 2016 at 9:34 PM, Jeffrey Burdick notifications@github.com wrote:

I tried a pull request, but I'm not very good at these things :(

CQ 2.0 sounds exciting. I'll be honest, I don't find CQ as it currently sits very human legible in comparison to OpenSCAD. I hope CQ 2.0 is a bit easier to read and write.

I HATE that OpenSCAD can't make STEP files. How am I supposed to CNC my parts if round features aren't actually round?!

— You are receiving this because you commented. Reply to this email directly or view it on GitHub https://github.com/dcowden/cadquery/issues/141#issuecomment-210705817

dcowden commented 5 years ago

Comment by burdickjp Saturday Apr 16, 2016 at 13:24 GMT


Feature trees are exciting!

I agree that Python makes things much, much more powerful, but in the case of CQ I don't think it makes it more human readable than OpenSCAD. There are many, many disadvantages to OpenSCAD, but I don't think syntax is one. In this case I find the additional space useful. It may not be a fault of the syntax of CQ, though. I may be getting lost in the coding conventions used in the examples. Here's one of the examples from parametric parts (http://www.parametricparts.com/docs/examples.html#id15):

result = Workplane("front").box(4.0,4.0,0.25).faces(">Z").workplane()  \
     .transformed(offset=cad.Vector(0,-1.5,1.0),rotate=cad.Vector(60,0,0)) \
     .rect(1.5,1.5,forConstruction=True).vertices().hole(0.25)

Even after seeing what it's supposed to be I'm having trouble following it. Here's my best guess:

result =  Workplane("front").box(4.0,4.0,0.25) \ #constructs a box on the YZ plane in the Positive Y direction
.faces(">Z").workplane() \ #selects the face parallel to XY with the largest Z value and makes a workplane
.transformed(offset=cad.Vector(0,-1.5,1.0),rotate=cad.Vector(60,0,0)) \  #offsets and rotates that workplane. What's with Vector? Why not use a conventional python list [0,-1.5,1.0]?
.rect(1.5,1.5,forConstruction=True).vertices().hole(0.25) \ use the points of a construction rectangle as the center points of holes.
dcowden commented 5 years ago

Comment by dcowden Saturday Apr 16, 2016 at 14:26 GMT


Yeah, the compactness is a matter of convention rather than necessity. You can split the cq code into more separate lines with more descriptive variable names if that's your preference.

Compactness vs readability is usually aatter of comfort with the API, and to what extent you care about readability for those who may read the code later on. For enterprise coding, u would definitely use a much less compact style thN in some of the samples, I agree.

But, all that said it think the cq functions do a good job of minimizing the amount of code you have to write to achieve a result.

I'm open to suggestions! On Apr 16, 2016 9:24 AM, "Jeffrey Burdick" notifications@github.com wrote:

Feature trees are exciting!

I agree that Python makes things much, much more powerful, but in the case of CQ I don't think it makes it more human readable than OpenSCAD. There are many, many disadvantages to OpenSCAD, but I don't think syntax is one. In this case I find the additional space useful. It may not be a fault of the syntax of CQ, though. I may be getting lost in the coding conventions used in the examples. Here's one of the examples from parametric parts ( http://www.parametricparts.com/docs/examples.html#id15):

result = Workplane("front").box(4.0,4.0,0.25).faces(">Z").workplane() \ .transformed(offset=cad.Vector(0,-1.5,1.0),rotate=cad.Vector(60,0,0)) \ .rect(1.5,1.5,forConstruction=True).vertices().hole(0.25)

Even after seeing what it's supposed to be I'm having trouble following it. Here's my best guess:

result = Workplane("front").box(4.0,4.0,0.25) \ #constructs a box on the YZ plane in the Positive Y direction .faces(">Z").workplane() \ #selects the face parallel to XY with the largest Z value and makes a workplane .transformed(offset=cad.Vector(0,-1.5,1.0),rotate=cad.Vector(60,0,0)) \ #offsets and rotates that workplane. What's with Vector? Why not use a conventional python list [0,-1.5,1.0]? .rect(1.5,1.5,forConstruction=True).vertices().hole(0.25) \ use the points of a construction rectangle as the center points of holes.

— You are receiving this because you commented. Reply to this email directly or view it on GitHub https://github.com/dcowden/cadquery/issues/141#issuecomment-210814493

dcowden commented 5 years ago

Comment by dcowden Saturday Apr 16, 2016 at 15:06 GMT


I wanted to respond to your code example question when i had a full keyboard :)

You are pretty much exactly right with your analysis of the CQ code. Here's a version that is much less compact, and uses variable names to convey intent. this is more appropriate for someone less familiar with the CQ api:

Compact Version:

result = Workplane("front").box(4.0,4.0,0.25).faces(">Z").workplane() \

 .transformed(offset=cad.Vector(0,-1.5,1.0),rotate=cad.Vector(60,0,0)) \

 .rect(1.5,1.5,forConstruction=True).vertices().hole(0.25)

Longer Version:

workplane = Workplane("front")

baseBox = workplane.box(4.0,4.0,0.25)

topMostFace = baseBox.faces(">Z")

workplaneOnTopFace = topMostFace.workplane()

rotatedWorkplane = workplaneOnTopFace.transformed(offset=cad.Vector(0,-1.5,1.0), rotate=cad.Vector(60,0,0) )

cornerPoints = rotatedWorkplane.rect(1.5,1.5,forConstruction=True).vertices()

baseWithHoles = cornerPoints.hole(0.25 )

I have also noticed that sometimes people are more comfortable with boolean operations. This is because you need to know less background about CQ to get going.

For example, in the above, one thing you have to 'know' is that the hole() operation will automatically create cylinders and then subtract them. The form hole(...) is definitely more compact than creating cylinders and subtracting them, but also requires more background knowledge. CQ allows you to do it the more obvious way if you want:

workplane = Workplane("front")

baseBox = workplane.box(4.0,4.0,0.25)

topMostFace = baseBox.faces(">Z")

workplaneOnTopFace = topMostFace.workplane()

rotatedWorkplane = workplaneOnTopFace.transformed(offset=cad.Vector(0,-1.5,1.0), rotate=cad.Vector(60,0,0) )

cornerPoints = rotatedWorkplane.rect(1.5,1.5,forConstruction=True).vertices()

holesToCut = cornerPoints.circle(0.25 ).extrude(-4.0)

baseWithHoles = baseBox.subtract(holesToCut)

It is a very 'CQ' thing to offer hole(), which prevents the need to make a cylinder, subtract it, and then compute how long it has to be in order to clear all of the material. The syntax is more compact, but initially harder to understand i agree.

On Sat, Apr 16, 2016 at 9:24 AM, Jeffrey Burdick notifications@github.com wrote:

Feature trees are exciting!

I agree that Python makes things much, much more powerful, but in the case of CQ I don't think it makes it more human readable than OpenSCAD. There are many, many disadvantages to OpenSCAD, but I don't think syntax is one. In this case I find the additional space useful. It may not be a fault of the syntax of CQ, though. I may be getting lost in the coding conventions used in the examples. Here's one of the examples from parametric parts ( http://www.parametricparts.com/docs/examples.html#id15):

result = Workplane("front").box(4.0,4.0,0.25).faces(">Z").workplane() \ .transformed(offset=cad.Vector(0,-1.5,1.0),rotate=cad.Vector(60,0,0)) \ .rect(1.5,1.5,forConstruction=True).vertices().hole(0.25)

Even after seeing what it's supposed to be I'm having trouble following it. Here's my best guess:

result = Workplane("front").box(4.0,4.0,0.25) \ #constructs a box on the YZ plane in the Positive Y direction .faces(">Z").workplane() \ #selects the face parallel to XY with the largest Z value and makes a workplane .transformed(offset=cad.Vector(0,-1.5,1.0),rotate=cad.Vector(60,0,0)) \ #offsets and rotates that workplane. What's with Vector? Why not use a conventional python list [0,-1.5,1.0]? .rect(1.5,1.5,forConstruction=True).vertices().hole(0.25) \ use the points of a construction rectangle as the center points of holes.

— You are receiving this because you commented. Reply to this email directly or view it on GitHub https://github.com/dcowden/cadquery/issues/141#issuecomment-210814493

dcowden commented 5 years ago

Comment by jmwright Saturday Apr 16, 2016 at 17:21 GMT


I tried a pull request, but I'm not very good at these things :(

@burdickjp No worries, we're glad to have the contribution. We're happy to work with contributors to get pull requests ready to merge.

dcowden commented 5 years ago

Comment by burdickjp Saturday Apr 16, 2016 at 19:09 GMT


@dcowden Your long-form example helps a lot! Maybe, more than anything, the examples need effort more than the CQ codebase. Something I don't like about the long-form example is that you end up with a string of unnecessarily named variables at the end. Here's yet another way to look at it, which may be some kind of (unnecessary) compromise between the two, using comments to describe the function of a line of code rather than having variables, but still having things spread out.

p = Workplane("front") \ #workplane
    .box(4.0,4.0,0.25) \ #baseBox
    .faces(">Z").workplane() \ #workplaneOnTopFace
    .transformed(offset=cad.Vector(0,-1.5,1.0), rotate=cad.Vector(60,0,0) ) \ #rotatedWorkplane
    .rect(1.5,1.5,forConstruction=True).vertices() \ #cornerPoints
    .hole(0.25 ) \ #baseWithHoles
dcowden commented 5 years ago

Comment by burdickjp Saturday Apr 16, 2016 at 19:10 GMT


@jmwright I just put in another pull request. Hopefully a much better one.

dcowden commented 5 years ago

Comment by dcowden Saturday Apr 16, 2016 at 20:08 GMT


Yep, I think you have the hang of it now...

Please let me know of further language improvements you would suggest. I will try to merge your PR tonight.

If you are interested and willing, we would love help on cq 2.0.... On Apr 16, 2016 3:09 PM, "Jeffrey Burdick" notifications@github.com wrote:

@dcowden https://github.com/dcowden Your long-form example helps a lot! Maybe, more than anything, the examples need effort more than the CQ codebase. Something I don't like about the long-form example is that you end up with a string of unnecessarily named variables at the end. Here's yet another way to look at it, which may be some kind of (unnecessary) compromise between the two, using comments to describe the function of a line of code rather than having variables, but still having things spread out.

p = Workplane("front") \ #workplane .box(4.0,4.0,0.25) \ #baseBox .faces(">Z").workplane() \ #workplaneOnTopFace .transformed(offset=cad.Vector(0,-1.5,1.0), rotate=cad.Vector(60,0,0) ) \ #rotatedWorkplane .rect(1.5,1.5,forConstruction=True).vertices() \ #cornerPoints .hole(0.25 ) \ #baseWithHoles

— You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub https://github.com/dcowden/cadquery/issues/141#issuecomment-210875883

dcowden commented 5 years ago

Comment by jmwright Saturday Apr 16, 2016 at 20:13 GMT


@burdickjp I like the inline comments after the line escapes, and I can't think of a reason why they would cause problems.

@dcowden What do you think about making a task to reformat the examples this way? Anything we can do to help new users is a win.

dcowden commented 5 years ago

Comment by burdickjp Saturday Apr 16, 2016 at 20:19 GMT


@dcowden I'm willing to help.

@jmwright If you make a task to reformat the examples I am willing to go through them. This sounds like a great way to both familiarize myself with CQ and contribute.

dcowden commented 5 years ago

Comment by dcowden Saturday Apr 16, 2016 at 23:05 GMT


Sure it sounds like a great idea to me.... On Apr 16, 2016 4:13 PM, "Jeremy Wright" notifications@github.com wrote:

@burdickjp https://github.com/burdickjp I like the inline comments after the line escapes, and I can't think of a reason why they would cause problems.

@dcowden https://github.com/dcowden What do you think about making a task to reformat the examples this way? Anything we can do to help new users is a win.

— You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub https://github.com/dcowden/cadquery/issues/141#issuecomment-210887635

dcowden commented 5 years ago

Comment by burdickjp Sunday Apr 17, 2016 at 02:53 GMT


@dcowden Are those ellipses of confidence or ellipses of hesitation? Where should I try to get started?

dcowden commented 5 years ago

Comment by jmwright Sunday Apr 17, 2016 at 02:56 GMT


@burdickjp In about 20 minutes I'm planning to sit down and create an issue for this. I'll outline some thoughts on how to get started.

dcowden commented 5 years ago

Comment by jmwright Sunday Apr 17, 2016 at 03:55 GMT


@burdickjp I started writing up an issue, but the comments after the line continuations keep throwing errors for me. Is there a trick to it?

dcowden commented 5 years ago

Comment by burdickjp Sunday Apr 17, 2016 at 11:49 GMT


I will have to admit I hadn't tried it in a Python environment. But I'm seeing the error. It seems Python doesn't want anything after a \