Creating Components in Cab.rb
-
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
-
@frankn said:
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?
ALL
Sketchup::ComponentDefinition
objects are kept in the model'sSketchup::DefinitionList
collection. ALL of them, regardless of what level any instances (Sketchup::ComponentInstance
,) of them are inserted.
The "level" can at the top (under the model,) or nested many levels deep beneath any combination of Group / ComponentDefinition "parents".The
Entity.parent()
method is inherited (down thruSketchup:Drawingelement
,) to all the objects that can be put into a model.So create all the component definitions you will use, and load them into the model.
To nest a component into another, insert an instance of the subcomponent, into theentities
collection of another component's definition.outer_comp_def.entities.add_instance( inner_comp_def, transform )
.. repeat to suit (ie, you can insert multiple bolt & nut component instances, in another component definition.)
P.S. (a
Group
is a "special" component, and so it also has aSketchup::ComponentDefinition
) -
Hi Dan,
Thanks for the explanation, I think that's what I did without realizing with just trial and error... many, many errors!
I thought you might like to see what I've been working on and by default bothering you with so I included the script.
Keep in mind this is a work in progress and my first attemp at at sort of programming so go easy on me. and please feel free to comment on what I'm doing wrong or that I could improve.
Also the Drawers and Doors functions/inputs aren't implemented yet... still haven't figured that part out.
Thanks again,
Frank
-
I fixed a few things and realized I could call the instances I added to the global component by different names.
It does run slow but I still haven't looked into optimising scripts so hopefully I can speed it up some though I don't mind it though, it's faster then creating a cabinet from scratch.
Next on the to do/ to learn list is...
- Creating door and drawer fronts, creating drawers... shouldn't be a problem.
- Add a user defined number of shelves and insert them evenly spaced in the cabinet... I can do this manually/individually but the math involved in doing them automatically should be fun to figure out!
- Same thing for door and drawer fronts.
- And give a choice of creating an upper or lower cabinet since the building of these is different... I could use 2 different scripts like I did when moding the cab.rb script but I think it would be slick having it all-in-one.
- Add grain direction
- And eventually learning how to export the to a csv file so that I can populate an existing excel spreadsheet that I use... yes I know there are already some cut sheet/list scripts but this is more of a hobby/learning experience and I'd like to see what I can do.
That should be it but I'm already happy with the progress I've made in just 3 or 4 days or learning this stuff.
Again thanks for all the help I really appreciate your teach how to fish approach.
Frank
Advertisement