Naming an entity
-
I've just started coding in ruby for sketchup and this forum really rock on helping me getting started.
I wanted to created a line, name it, set attribute to it and be able to return to that particular line ( I mean selecting it) by stating its name in ruby script after sketchup is re-started. Do I need to make the line into group or component in order to do it ?
Can I export the entity name ( in this case a line) and its attribute to a file (a csv or database file) ? If database can be used, what type of database ? Can I use MySQL and how do I read the database file into sketchup ?
Thanks guys for all the help in this forum.
-
Welcome here satay,
I think you should post in the Developer's forum http://forums.sketchucation.com/viewforum.php?f=180.
-
I've moved this into its proper place in the Developers' Forum.
You can only 'name' groups, component-definitions, component instances, materials, layers etc as this list shows.https://developers.google.com/sketchup/docs/methods#index_n
You can get the names of more things than you can assign names to in code...So you can't create a line [Edge] and 'name' it however you could assign an attribute to it and find it using that... However, be aware that over-drawing a line or perhaps an erase+undo might well loose the original edge and thereby its 'name' attribute...
Assuming you have an edge using the reference 'edge'... then use...
edge.set_attribute("Satay","edge_name","fred")
or whatever name you want to assign...
repeat for other edges with different names...
then later on...
edge_names = {} Sketchup.active_model.entities.each{|e| next unless e.is_a?(Sketchup::Edge) edge_names[e.get_attribute("Satay","edge_name","")] = e }
Now you have a hash of all edges in the model by 'name'.
Sotest_edge = edge_names["fred"]
should return an edge that's assigned that name, or 'nil' if there's no match for the name. If there are edges with the same name then the last one found will be returned as 'test_edge'.Now do stuff with the matching edge, if found...
-
Thank you very much TIG for the prompt and in depth reply.
There is a lot of info to digest and I will try it out.
Thank you again.
Advertisement