[Question] how to add entities to componentdefinitions
-
Hi all
as a beginner, I have a simple question I guess
Im looking how to "intialize" entities into a component at startup.
As far as I know its possible to use something like componetdefintions??For instance I want to make a reusable box I can use again and again.
I dont want it to load it from a file.tnx in advance
RVS
-
Create a component:
model.definitions.add
http://code.google.com/apis/sketchup/docs/ourdoc/definitionlist.html#addmodel = Sketchup.active_model box = model.definitions.add('box') face = box.entities.add_face( [0,0,0], [10,0,0], [10,10,0], [0,10,0] ) face.pushpull( 10 )
If you want to create such a component every time you open a file you need to use the AppObserver to catch the events when a new model is made:
http://code.google.com/apis/sketchup/docs/ourdoc/appobserver.html -
You need to give a bit more detail before we can assist you fully.
If you always want particular components to be in any new SKP then simply have them pre-loaded in the default SKP template that opens when you make anew file.
If you want to 'load' or 'import' components from an external SKP file it's straightforward as long as you know the path/skp_name etc - see http://code.google.com/apis/sketchup/docs/ourdoc/definitionlist.html#load and http://code.google.com/apis/sketchup/docs/ourdoc/model.html#import.
You can make a new definition usingnewcdef=model.definitions.add
[ http://code.google.com/apis/sketchup/docs/ourdoc/definitionlist.html#add ] - then change its name etc usingnewcdef.name="new_name"
etc... Then you can add entities to the definition using theentities.add_...()
[ http://code.google.com/apis/sketchup/docs/ourdoc/entities.html ] which allow you to add various kinds of geometry and so on - even instances of sub-components inside definitions etc etc...
Once you have the component-definition in your SKP you useentities.add_instance(cdef, trans)
- where cdef is the component-definition and trans is its 'transformation' - typically its insertion-point [although you could move/scale/rotate it etc too] - see http://code.google.com/apis/sketchup/docs/ourdoc/transformation.html.
If you have a component-definition that you want to change - e.g. by adding or deleting entities from within it - then theentities.add_...()
[ http://code.google.com/apis/sketchup/docs/ourdoc/entities.html ] orentity.erase!
[ http://code.google.com/apis/sketchup/docs/ourdoc/drawingelement.html#erase! ] methods etc will help...
Remember that if you change a component-definition it will affect all instances of it in the model... unless you have first used theinstance.make_unique
method on the one instance you want changing... and then got it's new definition withcdef=instance.definition
and you then change the entities of that definition.
If you have several similar component-definitions with instances placed in the model remember that you can simply swap them around usinginstance.definition=new_definition
[Tip: having common insertion points helps !]...
-
Thanks to both of you.
It pretty much answer all my questions for now!btw, SU and ruby combined makes me breathless. I got a tons of ideas, but only
beginning to learn. -
@rvs1977 said:
btw, SU and ruby combined makes me breathless. I got a tons of ideas, [...]
Welcome to the club! Hope you will enjoy the ride. Keep asking any questions you might have.
-
It looks like I'm treading ground that's been settled for years.
I thought your link to the entities class was going to solve my problem, but...
I'm trying to construct component definitions out of several previously defined components, but without placing any physical objects.
Here's one of those definitions:` # Make the bottom plate
bottom_plate_def = Sketchup.active_model.definitions.add "Bottom Plate"
bottom_plate = bottom_plate_def.entities.add_face pt # pt is an array of pointsMove the origin to center of final 3D cell
tr = Geom::Transformation.translation [-oal/2, -oaw/2, -cell_thickness/2 + plate_thickness]
bottom_plate_def.entities.transform_entities tr, bottom_plate`Now, how do I combine three such definitions (bottom_plate, core, top_plate) into a fourth one? The entities class doesn't seem to allow it. But it must be possible, since it's a straightforward operation in Sketchup itself.
three_d_cell_std_def = Sketchup.active_model.definitions.add "3D Cell - Standard"
three_d_cell_std_def = Sketchup.active_model.definitions.add "3D Cell - Standard"
-
You already know how to add objects to an '.entities' collection [i.e. for model/active_/definition/group].
Make a new definition, then use defn.entities.add_instances of the other three [already made] components as you wish...
OR make the containing definition, then make gp=defn.entities.add_group() and add geometry etc to gp.entities, make the group into a component and then adjust its definition name etc as needed - repeat three times for the contained instances...
Advertisement