Naming components

Naming components

Postby 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. :oops:

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
0

Frankn 
 

Re: Naming components

Postby 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. :oops:

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 ??
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Naming components

Postby 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.
0
User avatar
Dan Rathbun 
 

Re: Naming components

Postby Dan Rathbun » Sat Feb 25, 2012 12:52 pm

And you should be using an editor like Notepad++ that has syntax highlighting, to avoid attempting to use keywords as variable names.

http://notepad-plus-plus.org/download/

see: [Info] Notepad++ : Tip, Tricks & Plugins
0
User avatar
Dan Rathbun 
 

Re: Naming components

Postby 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 :oops: :D . 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. :? :lol:

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
0

Frankn 
 

Re: Naming components

Postby 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'...
:?
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Naming components

Postby 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)
0

Frankn 
 

Re: Naming components

Postby TIG » Sat Feb 25, 2012 9:48 pm

dins=model.active_entities.add_instance(defn, tr)
dins.name=name

The 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 :?
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Naming components

Postby Frankn » Sat Feb 25, 2012 10:19 pm

I knew that model.place_component was too good to be true! :D

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!
0

Frankn 
 

Re: Naming components

Postby 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 Keywords

Frankn 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.
0
User avatar
Dan Rathbun 
 

Re: Naming components

Postby 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. :D

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!
0

Frankn 
 

SketchUcation One-Liner Adverts

by Ad Machine » 5 minutes ago



Ad Machine 
Robot
 



 

Return to Developers' Forum

Who is online

Users browsing this forum: skuca91 and 5 guests

Visit our sponsors: