Creating Components in Cab.rb
-
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 -
@frankn said:
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.
Oh are these YOUR components ?? (ie, NOT Google supplied components,) that your having problems with?
See API: ComponentDefinition.refresh_thumbnail
cdefs = Sketchup.active_model.definitions cdefs.each {|comp| comp.refresh_thumbnail }
-
Also..
-
open the Model Info dialog.
-
Go to the File panel.
-
check the "Redefine thumbnail on save" option
-
-
See API: ComponentDefinition.refresh_thumbnail
cdefs = Sketchup.active_model.definitions cdefs.each {|comp| comp.refresh_thumbnail }
[/quote]
Thanks that worked perfect.
Frank
Advertisement