Creating Components in Cab.rb
-
Hi,
We have started a kitchen making business, and I have been elected to the front office: quotes, customer relations, cold calls, MYOB accounts. And now, drawings. They can make their own coffee. I'm drawing the line at coffee. ha ha.
I shoppped around on the internet and found the best one (user friendly) was free. By far, at least for a beginner!
Carcasses is the thing I draw up the most that I make little mistakes with. So I wanted to automate it. I found cab.rb in the depot library, and changed the co-ordinates around to suit. I changed the middle panel to a back panel, and changed it around so that the side panels were not extensions of the kickers, etc. I thought I was quite the resoucesful one.
My problem is that the drawing process and the feel of the the drawing has to reflect the actual cutting and putting together in the workshop. When I do it the slow way without Ruby I draw the lines for a single piece of timber, Select All, Make Component, then move to the next cut. If I do it in that sequence, then a cabinet will be all joined together (like in the workshop), but I will have the ability to highlight each component (each cut of wood), change it, colour it, measure it, or whatever.
When I use Cab.rb, its like there was never that process, what appears is the whole cabinet as one as-yet single piece. Obviously it would be a waste for me to laboriously go back and select each line and each face of each peice of wood and press the "Make Component" button.
I'm having trouble knowing the right words so I can search for the answer. Every time I looked up "Make Component" it took me back to the normal menu on the Sketchup page, and not ruby script.
I hope there is a simple solution. Thanks in advance.
Here is a bit of the script. I need each panel and each kicker and each door to be a single component:
model = Sketchup.active_model
model.start_operation "Create Cab"
entities = model.active_entities
group = entities.add_group
entities = group.entitiesls=[] #left panel ls[0] = [0, depth, kick] ls[1] = [0, depth, height-thick] ls[2] = [0, thick, height-thick] ls[3] = [0, thick, kick] ls_base = entities.add_face ls ls_base.pushpull thick x = width-thick rs=[] #right panel rs[0] = [x, depth, kick] rs[1] = [x, depth, height-thick] rs[2] = [x, thick, height-thick] rs[3] = [x, thick, kick] rs_base = entities.add_face rs rs_base.pushpull thick rp=[] #rear panel rp[0] = [0, depth, kick] rp[1] = [0, depth+thick, kick] rp[2] = [0, depth+thick, height-thick] rp[3] = [0, depth, height-thick] rp_base = entities.add_face rp rp_base.pushpull -width top=[] #top top[0] = [thick, depth-10.cm, height-thick] top[1] = [thick, depth, height-thick] top[2] = [thick, depth, height-thick-thick] top[3] = [thick, depth-10.cm, height-thick-thick] top_base = entities.add_face top top_base.pushpull width-thick-thick btm=[] #bottom shelf btm[0] = [thick, thick, kick+thick] btm[1] = [thick, depth, kick+thick] btm[2] = [thick, depth, kick+0] btm[3] = [thick, thick, kick+0] btm_base = entities.add_face btm btm_base.pushpull width-thick-thick kickf=[] #kicktoe panel front kickf[0] = [0, 4.cm, 0] kickf[1] = [0, 4.cm+thick, 0] kickf[2] = [0, 4.cm+thick, kick] kickf[3] = [0, 4.cm, kick] kickf_base = entities.add_face kickf kickf_base.pushpull -width kickb=[] #kicktoe panel back kickb[0] = [0, depth-4.cm, 0] kickb[1] = [0, depth-4.cm-thick, 0] kickb[2] = [0, depth-4.cm-thick, kick] kickb[3] = [0, depth-4.cm, kick] kickb_base = entities.add_face kickb kickb_base.pushpull -width
#-----------------------------------------------------------------------------
view = model.active_view
view.zoom_extents .... -
While you are waiting for plugin update consider using http://forums.sketchucation.com/viewtopic.php?f=323&t=14975&hilit=selection+toys%E2%80%A6. This allows for much quicker selection.
-
In SketchUp, drawing elements are added to an Entities collection. The Model has an Entities collection, and also Groups and ComponentDefinitions.
So if before each line that has entities.add_[something], if you create a new ComponentDefinition and add the drawing elements to that Entities collection.
An example:ls=[] #left panel ls[0] = [0, depth, kick] ls[1] = [0, depth, height-thick] ls[2] = [0, thick, height-thick] ls[3] = [0, thick, kick] new_definition=Sketchup.active_model.definitions.add("Left Panel") cdef_entities = new_definition.entities ls_base = cdef_entities.add_face(ls) ls_base.pushpull(thick)
-
Thanks Jim,
but I dropped that into the script, and when I ran the plugin it came back with nothing. Nothing appeared. I tried doing just the example you gave - that didn't work - and then each new element the extra lines. I made sure each one was different. ie:
ls=[] #left panel
ls[0] = [0, depth, kick]
ls[1] = [0, depth, height-thick]
ls[2] = [0, thick, height-thick]
ls[3] = [0, thick, kick]
new_definition=Sketchup.active_model.definitions.add("Left Panel")
cdef_entities = new_definition.entities
ls_base = cdef_entities.add_face(ls)
ls_base.pushpull(thick)and..
btm=[] #bottom shelf
btm[0] = [thick, thick, kick+thick]
btm[1] = [thick, depth, kick+thick]
btm[2] = [thick, depth, kick+0]
btm[3] = [thick, thick, kick+0]
new_definition=Sketchup.active_model.definitions.add("Bottom Shelf")
cdef_entities = new_definition.entities
btm_base = cdef_entities.add_face(btm)
btm_base.pushpull(width-thick-thick)Didn't work.
-
Sorry - it worked, but I seem to have left out the part where you insert the ComponentInstance into the model.
# Not the cdef_entities... but the model.entities or model.active_entities. entities.add_instance(new_definition, transformation)
-
Jim, I know you must be right. But still, I can't get it to work my end.
Here is the file. If I cut and paste even just your first lines to replace the first piece, nothing comes up when I run the plugin. I put the old lines back, and it works.
-
Well, in light of not getting an answer I can use (at least with my knowledge base), I have figured out an inelegant solution for my problem. I have moved each piece .01mm from each other. The side panel is .01mm away from the bottom shelf and .01mm away from the table top, and the back panel...etc. So now I can point the cursor at each piece without the whole assembly being selected, select all, and make a component.
Slow, but by making up a plugin, at least all the lines are already drawn.
I did have a look at the suggestions, but they just wouldn't work for me, no matter where I put them in the script. And I did try. I tried to look for references and examples, but it was a labynrith on google, and quite often the only link was back here in this thread. LOL I thought it would be easier, since it is a basic action. But this experience for me here has been like asking what the motto says on a school blazer, only to be told to learn the latin language and find out that way.
-
Hi,
Sorry for digging up an old post but this is exactly the file I'm trying to modify to add components to. As per Dan Rathbun's advice I've read, searched tried to emulate other scripts and I just can't get it to work.
I just don't graspe the inner workings of how to create components and the more I search the more I get confused because there seems to be a number of different ways to do this and obviously none of which my ruby newbie brain is understanding.
Any help would be greatly appreciated,
Frank -
Frank,
First, add a new ComponentDefinition:
model = Sketchup.active_model definitions = model.definitions new_component_definition = definitions.add("Definition Name")
Then add geometry to the Definition's Entities:
new_component_entities = new_component_definition.entities new_component_entities.add_line([0, 0, 0], [10, 10, 10])
Finally, add an Instance of the Definition to the model (or current context)
model.active_entities.add_instance(new_component_definition, Transformation.new)
-
Thank you so much Jim for taking the time to answer such a seemingly simple thing to do.
I still get confused with this part...
entities = new_component_definition.entities
entities.add_line([0, 0, 0], [10, 10, 10])Is the second line equivelent to the following?
ls=[] #left panel
ls[0] = [0, depth, kick]
ls[1] = [0, depth, height-thick]
ls[2] = [0, thick, height-thick]
ls[3] = [0, thick, kick]
ls_base = entities.add_face ls
ls_base.pushpull thickIt's starting to make more sense.
Out of curiosity and in the name of learning how to do things why is this code different then what you gave before, which is what I've been trying to make work?
Frank
-
For Brenden (and Frank, and whomever...)
Get the Cutlister plugin from Dana Woodman (ishboo)
ver 1.2: http://www.extendsketchup.com/plugins/cutlister/Note: The one here on SCF is ver 1.1
-
@frankn said:
I still get confused with this part...
entities = new_component_definition.entities > entities.add_line([0, 0, 0], [10, 10, 10])
Yes it can be and I (personally,) try not to use method names as reference (ie, variable,) identifiers (although Ruby allows it,) it is very confusing for newbies. I would use a identifier that you would KNOW is a reference, and write the example like:
ents_ref = new_component_definition.entities ents_ref.add_line([0, 0, 0], [10, 10, 10])
@frankn said:
Is the second line equivelent to the following?
ls=[] #left panel > ls[0] = [0, depth, kick] > ls[1] = [0, depth, height-thick] > ls[2] = [0, thick, height-thick] > ls[3] = [0, thick, kick] > ls_base = entities.add_face ls > ls_base.pushpull thick
NO. The former produces a single Skecthup::Edge object (adding it to the model, into the current editing context: the Model entities, a Group or Component entities collection, or a sub-Group of a Group... etc.)
The latter, adds 12Edges
and 6Faces
.@frankn said:
It's starting to make more sense.
Well don't feel bad.. because Components are in the second semester course.
-
Thanks for the link Dan, I'll give it a try and see how it compares to CutList 4.1
So is it better to use... entities.add_line([0, 0, 0], [10, 10, 10]) ...which is something new I'll have to learn then what this script is using?
@unknownuser said:
Well don't feel bad.. because Components are in the second semester course.
So what you're saying is I'm trying to run before I can walk...
If there's a simple tutorial on components somewhere please point me to it, I'd really like to understand this aspect of scripting because it seems like something I'd use a lot of.
Well back to the books/web for me... wish me luck I need it so far what you and Jim have given me causes Sketchup to crash and I know it isn't the code you've given me but I think it's where I'm placing it into the script...
Serenity now!!!
Thanks,
Frank -
The more I read about how to create entities (I think I'm using the correct term) the more I realize that this script (cab.rb) probably isn't the best one to learn to script with.
Using entries like entities.add_line([0, 0, 0], [10, 10, 10]) seem to be much more useful, also more powerful and maybe even simpler to use when you get how they work.
Even if this script does hwta I want it to do, seems like building off of it to get the end result I want is more work then making something from scratch...
Back to the reading!
p.s. Mods if this is taking this thread off topic let me know and I'll stop posting here.
-
@dan rathbun said:
Yes it can be and I (personally,) try not to use method names as reference (ie, variable,) identifiers (although Ruby allows it,) it is very confusing for newbies. I would use a identifier that you would KNOW is a reference, and write the example like:
Good point, I've modified my example to avoid the use of a method name as a reference.
-
Hi just wanted to thank you both for taking the time to help me learn something new. I was able to get it to work but I couldn't get this line to work...
model.active_entities.add_instance(new_def_component, Transformation.new) I keep getting this error not sure why.
Error: #<NameError: uninitialized constant Transformation>But I am able to get it to work with this and it does what I need so far.
inst = ent.add_instance def_shelf, [0, 0, 0]I do have a question. I know how to create multiple instances of the same component and even make them unique if I need to... but how can I make the number of instances needed a variable?
Example... user is prompted for the number of boxes he wants to add, he enters 3, the scripts creates 3 boxes in different locations.
Frank
-
@frankn said:
... but I couldn't get this line to work...
model.active_entities.add_instance(new_def_component, Transformation.new)
I keep getting this error not sure why.
Error: #<NameError: uninitialized constant Transformation>Because the
Transformation
class is wrapped in theGeom
module, it is qualified asGeom::Transformation
, and you cannot refer to it as simplyTransformation
unless your code is executing within moduleGeom
(which is a no-no.)So.. from another namespace (module,) you must qualify the classes, so the interpreter can find them. It's like id'g me as:
Earth::UnitedStates::Florida::Brevard::Dan
@frankn said:
But I am able to get it to work with this and it does what I need so far.
inst = ent.add_instance def_shelf, [0, 0, 0]
Because the 2nd arg to
add_instance()
takes aGeom::Point3d
object, and the API extended the baseArray
class to be Comparable with both the APIGeom::Point3d
andGeom::Vector3d
classes. -
Again thank you for the explaination Dan... it almost makes sense to me!
Something strange is happening with the way the components are being displayed in the Components window in Sketchup. They're all there but there is no preview picture to the components. But if I create a component manually it shows up... an idea why?
I've also been looking into adding components to other components and all I found so far is some refernences to subcomponents and parent components but nowhere did I find how those work?
This has been a greta learning experience and I find half the battle is actually using the correct search terms but I'm getting there!!
-
@frankn said:
Something strange is happening with the way the components are being displayed in the Components window in Sketchup. They're all there but there is no preview picture to the components. But if I create a component manually it shows up... an idea why?
Try refreshing the thumbnails, thru the thumbnail menu (the left hand button with the drop-down.)
-
I had already tried that and it doesn't work, the only way to get them to show up is to edit the component then the preview shows up. Not a big deal but was just curious if I was doing something wrong with the script/code.
Thanks again sir,
Frank
Advertisement