by Frankn » Sat Feb 25, 2012 9:45 am
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 Defintion Name to the parent component. I've read and searched but can't find a solution. - Code: Select all
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
-
Frankn
-
- Posts: 64
- Joined: Sat Apr 25, 2009 5:53 am
- Name: Frank
by TIG » Sat Feb 25, 2012 12:15 pm
Frankn wrote: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. - Code: Select all
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. - Code: Select all
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 ??
TIG
-

TIG
- Global Moderator
-
- Posts: 13903
- Joined: Mon Nov 12, 2007 7:24 pm
- Location: Northumbria UK
- Name: TIG
- Operating system: Windows
- SketchUp version: 8
- License type: Pro
- SketchUp use: architecture
- Level of SketchUp: Advanced
by Dan Rathbun » Sat Feb 25, 2012 12:45 pm
You can assign a value to the name attribute to a particular Sketchup::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 a Sketchup::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 another Sketchup::ComponentDefinition. Instead, you attach Sketchup::ComponentInstance instances, (who are described by Sketchup::ComponentDefinition instances,) to ANOTHER Sketchup::ComponentDefinition instance, that has nested components.
SO.. the actual nested objects... are Sketchup::ComponentInstance instances, never Sketchup::ComponentDefinition instances.
Think of a Sketchup::ComponentDefinition instance as being a specification, and all the Sketchup::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 the model.definitions() method.
-

Dan Rathbun
- Top SketchUcator
-
- Posts: 4069
- Joined: Tue Oct 06, 2009 3:06 am
- Location: Florida, USA
- Name: Dan Rathbun
- Operating system: Windows
- SketchUp version: 8
- License type: Pro
- SketchUp use: education
- Level of SketchUp: Advanced
by Frankn » Sat Feb 25, 2012 7:04 pm
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... viewtopic.php?f=180&t=23567- Code: Select all
group = 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... viewtopic.php?f=180&t=23311- Code: Select all
instance=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
-
Frankn
-
- Posts: 64
- Joined: Sat Apr 25, 2009 5:53 am
- Name: Frank
by TIG » Sat Feb 25, 2012 8:12 pm
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
-

TIG
- Global Moderator
-
- Posts: 13903
- Joined: Mon Nov 12, 2007 7:24 pm
- Location: Northumbria UK
- Name: TIG
- Operating system: Windows
- SketchUp version: 8
- License type: Pro
- SketchUp use: architecture
- Level of SketchUp: Advanced
by Frankn » Sat Feb 25, 2012 8:24 pm
TIG wrote: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 - Code: Select all
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)?... - Code: Select all
status=model.place_component(defn)
-
Frankn
-
- Posts: 64
- Joined: Sat Apr 25, 2009 5:53 am
- Name: Frank
by TIG » Sat Feb 25, 2012 9:48 pm
dins=model.active_entities.add_instance(defn, tr) dins.name=nameThe model.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 instance defn.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... 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 of din reset dynamically to always be at the cursor's point, and when you left-click the mouse it'd set dins's transformation to be at the clicked point... BUT perhaps that is too complex for now 
TIG
-

TIG
- Global Moderator
-
- Posts: 13903
- Joined: Mon Nov 12, 2007 7:24 pm
- Location: Northumbria UK
- Name: TIG
- Operating system: Windows
- SketchUp version: 8
- License type: Pro
- SketchUp use: architecture
- Level of SketchUp: Advanced
by Frankn » Sat Feb 25, 2012 10:19 pm
I knew that model.place_component was too good to be true! TIG wrote: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 of din reset dynamically to always be at the cursor's point, and when you left-click the mouse it'd set dins's transformation to be at the clicked point... BUT perhaps that is too complex for now 
Ok, 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
-
- Posts: 64
- Joined: Sat Apr 25, 2009 5:53 am
- Name: Frank
by Dan Rathbun » Sun Feb 26, 2012 12:39 am
Frankn wrote: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 KeywordsFrankn wrote: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.
-

Dan Rathbun
- Top SketchUcator
-
- Posts: 4069
- Joined: Tue Oct 06, 2009 3:06 am
- Location: Florida, USA
- Name: Dan Rathbun
- Operating system: Windows
- SketchUp version: 8
- License type: Pro
- SketchUp use: education
- Level of SketchUp: Advanced
by Frankn » Sun Feb 26, 2012 1:56 am
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!
-
Frankn
-
- Posts: 64
- Joined: Sat Apr 25, 2009 5:53 am
- Name: Frank
Return to Developers' Forum
|