Ruby Tutorial: How to create and add a component from Ruby
-
I've been asked a few times to explain how to add a component to a model from Ruby.
This short example shows the basics of how to do it. There are only a few steps required, but it's not readily intuitive when first getting started with Ruby. So, without further ado... here you go. (See attached script without all the formatting to run it yourself.)
**%(green)[# How to Create a component and add it to you model from Ruby.Written by Todd Burch www dot smustard dot com June 23, 2007.
First, create a new Component Definition. The name "MyCube" is arbitrary.]
new_comp_def = Sketchup.active_model.definitions.add("MyCube") ;
# Now, define some points for an arbitrary face.
%(blue)[points = Array.new ;
points[0] = ORIGIN ;]# "ORIGIN" is a SU provided constantpoints[1 = [100, 0 , 0] ;
points[2] = [100, 100, 0] ;
points[3] = [0 , 100, 0] ;]
%(green)[# Now, add the face to the new component definition.Don't add it to the model, as in Sketchup.active_model.entities.add_face... - but add it to the component definition.]
newface = new_comp_def.entities.add_face(points) ;%(green)[# returns a Face
If the blue face is pointing up, reverse it.]
newface.reverse! if newface.normal.z < 0 ;%(green)[# flip face to up if facing down
pushpull the face into a Cube]
newface.pushpull(100) ;%(green)[# extrude it up to make a cube
To add the component, you have to define a transformation. We can define
a transformation that does nothing to just get the job done.]
trans = Geom::Transformation.new ;%(green)[# an empty, default transformation.
Now, insert the Cube component.]
Sketchup.active_model.active_entities.add_instance(new_comp_def, trans) ;**
Obvioulsy, this can be expanded upon to be more interesting.
There is another way to add a component to a model, and that is through the model.place_component method. The place_component method allows the user to left-click where the component will be placed. You can define the insertion_point so that the specific insertion point of the component is at the tip of the cursor.
Todd
-
Seems to me that maybe we should create a "Ruby tutorial subforum" here.
Thanks Todd! -
@gaieus said:
Seems to me that maybe we should create a "Ruby tutorial subforum" here.
Thanks Todd!I agree...that would deal with a great many of the problems that new users have with rubies. The available Ruby texts you can buy do not deal with the implementation of Ruby in SketchUp.
--Lewis
[Lewis Wadsworth]
-
I find these little "Ruby snippets" very helpful. they are a great way to learn to code in Ruby.
I have another idea for a snippet. Using the Cube as a basis that Todd started with above.How do I code a radial array of cubes about a central point?
tia
-
@tomot said:
How do I code a radial array of cubes about a central point?
tiaLook into my "Grow.rb" script to see how to do all types of matrix arraying etc - Move, Rotate and Scale of copies...
-
I revisited the Cube this afternoon:
I added a dialog box, 2 sub-menus.
One for a component cube, the other
for a grouped cube. Then added some color
and transparency statements.Thanks TIG I will have a look at grow.rb
btw: using the component cube first , followed by grow.rb
and then changing any component cube creates some interesting effects.
-
I'm still doing some further investigating into components used in Ruby
The attached Ruby could produce 2 options for component editing, by adding a sub menu.What works now is that each component in the group can be edited on its own
What does not work!
Editing one component in the group , and had have the other 8 follow the same editing changes.I hope someone can have a look at the code, I cant seem to find the right component code lines.
tia
-
If you make a component and place it several times then anything you do to one instance affects all of the others - unless you use 'make_unique' on it first. Using groups and copying them avoids this, BUT a recently noted but undocumented quirk in the SUp Ruby coding means you must use 'make_unique' on a group as you copy it... otherwise that copy is treated as an 'instance' (just as if it were a component anyway !!!) and then both might change as you alter one...
-
I'm wondering why you would suggest Make_unique
I was copying similar code from JoistTool.rb which allows for
component editing of the type I'm looking for.Yet JoistTool.rb does not contain any Make_unique code.
I'm wondering if the Upto statements are causing a problem?
-
But the Joist and Rafter tools make a component that is repeatedly copied AND if you subsequently edit one of them then they all change ! If you want parts that are unconnected then use Groups - but make each unique - as I said within a script they can be viewed as clones of the same thing and they'll edit together, but doing an edit in the "real world " has each of them treated as a single entity...
-
OK, I really don't understand at this point how or where I'm going to introduce make_unique, into my existing script. I have the following inserted before the 1st upto statement
definitions = model.definitions
component = definitions.add
entities = component.entitiesfollowed by:
trans = Geom::Transformation.new
entities = model.active_entities(component, trans)after the cube has been drawn
What I cant wrap my head around is:Both my script, Box4.rb and JoistTool.rb create copies of 3d entities.
JoistTool.rb uses t=Geom::Transformation.translation to make copies
while, Box4.rb uses Upto to make copies.Obviously these copies are treated differently in Ruby?
causing JoistTool.rb work, and Box4.rb not work?(I'm sorry for my thick head)
-
Try using groups as an alternative...
...
entities=model.active_entities
group=entities.add_group
gents=group.entities
...
gents.add_line(pt1,pt2)
gents.add_face(pt1,pt2,pt3...)etc, all made inside the group NOT model
then transform group etc
group1=group.copy
group1.make_uniquetransform group1 etc
...
-
I'm re-reading your comments, and somehow I get the feeling we are misunderstanding each other.
I think I'm responsible for that misunderstanding at the very beginning.your response to my question about the joist and rafter tools is a case in point.
I quote you:
"But the Joist and Rafter tools make a component that is repeatedly copied AND if you subsequently edit one of them then they all change !"
What I quoted above, is exactly what I would like my script to do. I'm not looking for parts that are unconnected, as you talk about in the remainder of your quote.
I would like all of the cubes that my script repeatedly copies/generates, to be made a component, so that if I then edit one all the rest will change.
-
Hi
I know it is nearly a year later, but your code is not doing what you think it is doing.
Box4 does the following logically:
It creates a component def
Then you add 1 face to it and pull the face to make a block
then you add this component as an instancethen you add another face to it and pull it making block 2
then you add another instance. But you also update the original instance to now have two blocksand so on
if you set it to have 3 by 3 blocks you get 9 components all the same with each nine pulled faces inside it. The inside blocks are not component instances, the are merely blocks (pulled faces) inside the component.
If you select the block at the end of the process and move it you will see there are more blocks underneath it.
If you wanted to do what you intended, you needed to create a new component def each time you enter the upto loop.
-
Its one of the things I have not been able to resolve.
I will give your suggestion a try, in the near future.thanks!
Advertisement