Naming components
-
@frankn said:
I'm having a problem with a seemingly simple task. how can I add both a Name and Defintion Name to a component that contains subcomponents?
I can add both to an instance of the subcomponents without a problem but I can only manage to add the Definition Name to the parent component.
I've read and searched but can't find a solution.
> def=model.definitions.add @name + " (test def)" > entities=def.entities > > ins=entities.add_instance @def_test, [0, 0, 0] > name=ins.name=@name + " (test ins)" >
Thanks,
Frank
You can't use 'def' as a reference - it's reserved for 'methods', try 'defn' instead.defn=model.definitions.add(@name + " (test def)") tr=Geom;;Transformation.new() ins=defn.entities.add_instance(@def_test, tr) name=ins.name=@name + " (test ins)"
Should work ??
-
You can assign a value to the
name
attribute to a particularSketchup::ComponentDefinition
as many times as you like, but after the first time, you will be re-assigning a new value.Keep in mind that, Rubywise, when you say definition, you really are talking about an instance of the
Sketchup::ComponentDefinition
class.And.. when you say, instance, we are talking about an instance of the
Sketchup::ComponentInstance
class.BOTH of these two class objects have an instance method, that allows assigning a (usually
String
) value, to an instance variable.
The method identifiers, are the same (and likely the attributes, ie, instance variables,) "name
".NOW... you may be a bit confused. Every
Sketchup::ComponentInstance
instance, has aSketchup::ComponentDefinition
instance, that describes it's properties, and if it is not unique, holds a list of all it's other siblings in the model.OK.. you do not attach OTHER
Sketchup::ComponentDefinition
instances to / beneath / inside of anotherSketchup::ComponentDefinition
. Instead, you attachSketchup::ComponentInstance
instances, (who are described bySketchup::ComponentDefinition
instances,) to ANOTHERSketchup::ComponentDefinition
instance, that has nested components.SO.. the actual nested objects... are
Sketchup::ComponentInstance
instances, neverSketchup::ComponentDefinition
instances.Think of a
Sketchup::ComponentDefinition
instance as being a specification, and all theSketchup::ComponentInstance
instances OF IT, as being the articles built TO the specification.Lastly... all the
Sketchup::ComponentDefinition
instances, are kept at the model level, (all in one drawer of specifications, so to speak.) Accessed via themodel.definitions()
method. -
And you should be using an editor like Notepad++ that has syntax highlighting, to avoid attempting to use keywords as variable names.
see: [url=http://forums.sketchucation.com/viewtopic.php?f=180&t=39783:2pgebma3][Info] Notepad++ : Tip, Tricks & Plugins[/url:2pgebma3]
-
Thanks for the replies.
Sorry for using def as a reference, I'm not using that in my plugin code it was just as an example... though I didn't know not to use it... learn something new everyday. I got used to komodo edit for some reason even though seems like Notepad ++ is what most use.
Now, that being said... Dan you're 100% correct I'm totally confused . And I'll have to reread your post a few times to get it straight, thanks for taking the time to explain the differences in detail.
TIG, using your code I get the same result as I am now. Which is my parent component has a definition name but no name and the child component has both. I know I'm probably not explain what I'm trying to do properly since as already stated I'm confused.
Here's something I found that resembles what I want to do other then I'm not working with groups...
From a post by ThomThom, here...
http://forums.sketchucation.com/viewtopic.php?f=180&t=23567group = model.active_entities.add_group(sel) name = model.definitions.unique_name(input[0]) # We set the name we will see in the Entities window. group.name = name # We set the definition name as well. If you convert the group to a component, this is the # name it will get. group.entities.parent.name = name
And also from TIG, here...
http://forums.sketchucation.com/viewtopic.php?f=180&t=23311instance=group.to_component definition=instance.definition definition.name="My Definition's Name" instance.name="My Instance's Name"
Hope that helps making what I'm trying to do clearer...
Thanks,
Frank -
The code I offered you was simply a corrected version of yours.
The code makes a new definition ['defn'] assigning it a new name [all definitions must have a unique name].
The code then adds an instance of another preexisting component [again that definition must have a unique name of its own] into the entities of 'defn'.
When that the instance is added it has no name [the default].
The code then gives that instance a name.
It also assigns a variable 'name', but does nothing with it.
The code then stops.If you were then to add an instance of 'defn' it would obviously display that component's definitions name [all instances of that definition will!]... However, an instance will not have a name, because by default a new instance does not have a name.
Of course you could give that instance any name you like... perhaps using 'name'...
-
@tig said:
The code I offered you was simply a corrected version of yours.
If you were then to add an instance of 'defn' it would obviously display that component's definitions name [all instances of that definition will!]... However, an instance will not have a name, because by default a new instance does not have a name.
Of course you could give that instance any name you like... perhaps using 'name'...
Ok so far I understand that logic and I'm aslo able to put it onto practice... except for the part I highligthed in bold... which correctly explains what I'm not able to do. I don't get where I should add the 'name' part. the only way I can think of is by
defn_new=entities.add_instance defn, [0, 0, 0]
but I get a double occurance error. And everything else I've tried will change the defintion name and not add an instance name.
Perhaps it's in how I add the instance of defn to the model that is the problem(I hope I'm using the correct term, still a little confused)?...
status=model.place_component(defn)
-
dins=model.active_entities.add_instance(defn, tr) dins.name=name
Themodel.place_component(defn)
is like picking 'defn
' off the Component-Browser.
You'll get no 'reference' to it this way - so naming it is difficult - because it'd be the only instancedefn.instances[0]
would refer to it, BUT the 'place_component
' will break your code when it's invoked - a convoluted observer set up might work BUT there are other easier ways...
Useadd_instance
and then if you want the instance [dins
] to be movable thereafter you can write your code as a 'Tool' class and then have the transformation ofdin
reset dynamically to always be at the cursor's point, and when you left-click the mouse it'd setdins
's transformation to be at the clicked point...
BUT perhaps that is too complex for now -
I knew that model.place_component was too good to be true!
@tig said:
Use
add_instance
and then if you want the instance [dins
] to be movable thereafter you can write your code as a 'Tool' class and then have the transformation ofdin
reset dynamically to always be at the cursor's point, and when you left-click the mouse it'd setdins
's transformation to be at the clicked point...
BUT perhaps that is too complex for nowOk, I have no idea what all that means but I'm game to learn it! Could you point me in the right direction? Maye a plugin that already does something similiar or a code snippet to get me started?
Thanks for the help TIG!
-
@frankn said:
Sorry for using
def
as a reference, I'm not using that in my plugin code it was just as an example... though I didn't know not to use it... learn something new everyday.They are called "reserved" keywords for good reason. Their misuse will confuse the poop out of the Ruby interpreter.
Here's a URL bookmark for ya':
Ruby Keywords@frankn said:
I got used to komodo edit for some reason even though seems like Notepad ++ is what most use.
Well as long as it has syntax highlighting for Ruby, that shows keywords, and classes in a different color than user defined variables... it's OK. I also know some people are partial to NetBeans.
That's what syntax highlighting is all about. Helping you to not make those kind of mistakes.
-
Thanks for the link Dan... that's the kind of info that seems trivial till you run into a problem.
Yeah Komodo does all that good stuff plus the pro version does a bunch of other stuff and even debugging for ruby, I think. But I tried it and it had too many bells and whistles for a newbie like me and was confusing!
Advertisement