Find the location of an object using Ruby
-
hmm... need more info.
Is the box in a component or group? If so - is it nested? Do you know the nesting?
You need the absolute co-ordinates in world space, or relative to its parent context?
-
bounds transformation
etc -
@thomthom said:
hmm... need more info.
Is the box in a component or group? If so - is it nested? Do you know the nesting?
You need the absolute co-ordinates in world space, or relative to its parent context?
It's a group, non nested and i'd like to be able to find the co-ordinates in world space. I've attached a simple example of what i'm looking at. It's just a grouped box. I'm trying to find the real world co-ordinates of the bottom left corner or center point...doesn't really matter as log as I can check the same spot consistently.
Thanks again!
Joe
-
Assuming that the grouped box is selected:
group = Sketchup.active_model.selection[0] bb = group.bounds
Now you can get it's center co-ordinates by:
bb.center
Or one of the corners: http://code.google.com/apis/sketchup/docs/ourdoc/boundingbox.html#corner
bb.corner(0)
-
@thomthom said:
Assuming that the grouped box is selected:
group = Sketchup.active_model.selection[0] bb = group.bounds
Now you can get it's center co-ordinates by:
bb.center
Or one of the corners: http://code.google.com/apis/sketchup/docs/ourdoc/boundingbox.html#corner
bb.corner(0)
That works! Now, though, what if it's not selected. Is there a way to reference the entity name so that at any given time (when it's not being touched) I could look up where it's location is?
-
Well, how is it being made? Is your script going to make the object? Does it need to be remembered every time the model is opened? Or does this only need to work while your script is running? There are ways to do all of this, but some are considerably harder than others.
If your script makes it, then you can just keep the group referenced with a variable. And find it easily while your script is running, or even until SU closes.
But if you need to find it again when SU is re-opened, then its a little tougher. You will need to add an attribute to your group that marks it as yours. And your script will need to parse through the model each time a model is opened and it will need to search the groups and components for any that are marked with your attibute.
So which is closer to what you think needs to happen?
Chris
-
@jblively said:
That works! Now, though, what if it's not selected. Is there a way to reference the entity name so that at any given time (when it's not being touched) I could look up where it's location is?
Yes - but how really depend on the scenario...
-
@chris fullmer said:
Well, how is it being made? Is your script going to make the object? Does it need to be remembered every time the model is opened? Or does this only need to work while your script is running? There are ways to do all of this, but some are considerably harder than others.
If your script makes it, then you can just keep the group referenced with a variable. And find it easily while your script is running, or even until SU closes.
But if you need to find it again when SU is re-opened, then its a little tougher. You will need to add an attribute to your group that marks it as yours. And your script will need to parse through the model each time a model is opened and it will need to search the groups and components for any that are marked with your attibute.
So which is closer to what you think needs to happen?
Chris
It will be an existing object that i'll need to find each time the model is opened. The group will eventually be used with sketchyphysics and will be moved by the user. I'll need to be able to get the groups x,y,z position at any given time.
In the 'entity info' box, can you give the group a name, such as 'mybox' and then be able to reference the 'mybox' group name somehow to locate it's x,y,z position from ruby?
Thanks guys...this is really helping!
-
Sure, I'm not exactly sure what people are doing to make plugins find objects as soon as a model is opened, but I think the options are to use an observer that checks for a new model being opened. Then your script would now to parse the components in the model and look for the one you want.
Another option could be to just have an initialize process in your script. Perhaps when you start your script, or maybe an actual menu item called "initialize". Or any number of ways to make you script find the object when the user loads your script for the first time.
I think the way you would want to parse the model for your component would be to parse the component definitions in the model, then look for the one you want. I would guess that adding an attribute to your component object would be the best thing to do. If it just realized on the name, then someone might change the name and your script would lose the ability to track the component. So whatever method you choose to "tag" or identify your component, just parse the definitions list for that. Then once you find the definition, get all its instances that are in the model (I think you're suggesting there will be just one). so find it, then you can work its co-ordinates.
Sounds like a fun project.
Chris
-
Give the "box" [group] an attribute to identify it.
box.set_attribute("jbl","this_is_the_one",true)
Then your early loading 'observer' can find it on the SKP's first opening and on any 'entities change' events... if an entity's a group and it's moved then use
if entity.get_attribute("jbl","this_is_the_one",false)....
then ...........
That way if a user renames it you still know it's there...
Copying it so you have multiple instances with the attribute is more tricky ??? -
hello gentlemen,
now the question is:how can i place my own object per ruby on
user defined coordinates (means: where the user definies per axis-position)
so the object's X-coordinate would be the AXIS's X and the 0,0,0 of the object
would be 0,0,0 of the USER's axis (temp.)i cannot find anything about axis-positioning in the api....
thanx & regards stan
thanx stan -
There is no [or at least very scant] API access to custom coordinates.
Do you really mean how do you move an object/group to a location ?
In that case you made a transformation and apply that to the object...
So let's say you want a group relocating at [1,2,3]...
tr=Geom::Transformation.new(Geom::Point3d.new(1,2,3)) group.transform!(tr)
It you have a group and you want a copy somewhere else, then you need its definition - use:
defn=group.entities.parent
If it's a component and you have an instance then:
defn=instance.definition
Then you can add_an instance to an entities context...
some_entities.add_instance(defn, tr)
If you are moving from one entities context into another and don't want the original use:
defn=group.entities.parent some_entities.add_instance(defn, group.transformation) group.erase!
you might need to adjust the transformation if the target context differs from the original ?
-
hi tig,
the situation is the following.when i calculate coordinates for the parts of my geometry to be created by the ruby, i have all measures related to 0,0,0.
but this is always the global zero.
what i would like to achieve is, that the user sets the axis at the point, where the new geometry is to be generated, with x-axis defining the orientation (the ruby would only take the temporary 0,0,0 , the direction of the horizontal compoment of the x-axis and place the object - always horizontal at X).
so if there are global variables showing the offset from global zero to temp-zero, i could place the object at global 0
and transform it (move).
stan -
So... if the parts are inside a group [with
[0,0,0]
(ORIGIN
) as its origin] then simply transform! the group to the new point, as explain before.
OR transform all of the entities inside the group using an 'offset' [in this case the group's origin stays where it was] - this time make a 'translation' transformation...
` vector=ORIGIN.vector_to(Geom::Point3d.new(1, 2, 3))or whatever 'offset' you want...
tr=Geom::Transformation.translation(vector)
then use:
group.entities.transform_entities(tr, group.entities.to_a)`
Now the group's contents move along that vector... -
hi tig,
exactly that is my problem.if i was defining the position for the object placement myself, i of course could transformate myself to a desired x,y,z-position.
but
what do i do, when the desired x,y,z is defined by the user (set coordinates axis) before he starts the script?
so the question is, is it possible to 'read' the user-defined axis-offset from some global variable?
stan
-
@artmusicstudio said:
hello gentlemen,
now the question is:how can i place my own object per ruby on
user defined coordinates (means: where the user definies per axis-position)
It is easier than you think.. using SketchUp's built-in place component tool.
But you must save your object as a component.
see my answer in another topic: http://sketchucation.com/forums/viewtopic.php?f=180&t=51853&p=469352&hilit=move+rotate+component#p469352
-
You can even do it with a group, BUT you need to write a Tool class...
Then after making the group you transform it to the cursor position and keep transforming it as the mouse moves. So it looks like it's stuck on the cursor, then when the user left-clicks the transforming stops and the group is where the user wishes...Probably easier, as Dan said, if you have made it as a group use
instance=group.to_component
thendefn=instance.definition
[usedefn.name='MyThing'
etc].
Theninstance.erase!
to remove the original and place the new component on the cursor for the user to choose a location withmodel.place_component(defn, false)
-
hi dan & tig,
thanx a lot, though this is a different approach than mine, but very useful. i will try to implement thiscurslr functionality , too, if i manage it.
for my idea i will let the user draw 1 line, which can maybe be selected for the ruby call and give the ruby the vector to transform the object.
so the ruby-fight continues...
stan -
You should study the drawline.rb in the "Examples" sub-dir.
Advertisement