sketchucation logo sketchucation
    • Login
    Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
    πŸ›£οΈ Road Profile Builder | Generate roads, curbs and pavements easily Download

    How to apply attributes through code? and how do they work?

    Scheduled Pinned Locked Moved Developers' Forum
    21 Posts 4 Posters 758 Views 4 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M Offline
      meeta
      last edited by

      here is the code i think its fine..

      
      def draw_beam
      		
      		model = Sketchup.active_model
      		entities = model.entities
      		group = entities.add_group
      		entities = group.entities
      	
      			pt = []
      			pt[0] = [0, 0, 0]
      			pt[1] = [@b, 0, 0]
      			pt[2] = [@b, 0, @h]
      			pt[3] = [0, 0, @h]
      	   
      		c_section = entities.add_face pt
      		 
      		@length = -@length if( c_section.normal.dot(Y_AXIS) < 0 )
      		
      		c_section.pushpull @length
      
      	entity1 = entities[1]
      	status = entity1.set_attribute "Dimensions", "Length", "#{@h} mm"
      	status = entity1.set_attribute "Dimensions", "Width", "#{@b} mm"
      	status = entity1.set_attribute "Dimensions", "Effective Length", "#{@d} mm"
          end
      

      Meet A

      1 Reply Last reply Reply Quote 0
      • thomthomT Offline
        thomthom
        last edited by

        "Well, there you're problem!" πŸ˜‰

        You are assigning the attributes to a random entity inside your group.

        group = entities.add_group
        Here: you have your group assigned the the variable group. This is the value you want to attach your attributes to.

        p.s.

        
        entities = model.entities
        group = entities.add_group
        
        

        This will always create the group at the top level of the model. If the user opens a group or component when this method is run, the user might expect the group to be created in this context - but your code will not do that. model.active_entities always refer to the current context.

        Thomas Thomassen β€” SketchUp Monkey & Coding addict
        List of my plugins and link to the CookieWare fund

        1 Reply Last reply Reply Quote 0
        • M Offline
          meeta
          last edited by

          Ahh there is my mistake! I love finding mistakes..

          I thought group is a method and not a variable!

          Meet A

          1 Reply Last reply Reply Quote 0
          • thomthomT Offline
            thomthom
            last edited by

            .add_group is a method. But it returns a variable - reference to the newly created group. http://code.google.com/intl/nb/apis/sketchup/docs/ourdoc/entities.html#add_group

            You created the variable group and assigned it to the return value of .add_group.

            Thomas Thomassen β€” SketchUp Monkey & Coding addict
            List of my plugins and link to the CookieWare fund

            1 Reply Last reply Reply Quote 0
            • M Offline
              meeta
              last edited by

              @beam = Sketchup.active_model.selection[0] bbox = @beam.bounds point1 = bbox.max point2 = bbox.min b = (point1[0] - point2[0]).abs.to_mm.to_i length = (point1[1] - point2[1]).abs.to_mm.to_i h = (point1[2] - point2[2]).abs.to_mm.to_i @beam.set_attribute "Beam Information", "1. Type of Concrete", "#{@cstrength}"

              Ok one problem now, I have selected active_model as either ComponentInstance or Group. When its a group I can apply the attributes with no problem! But when its a component I am not able to add the information. Is there a way I can add all this information to it? and Is there a way to apply the same attributes to all the ComponentInstances in the active_model?

              Meet A

              1 Reply Last reply Reply Quote 0
              • TIGT Offline
                TIG Moderator
                last edited by

                @meeta said:

                @beam = Sketchup.active_model.selection[0] bbox = @beam.bounds point1 = bbox.max point2 = bbox.min b = (point1[0] - point2[0]).abs.to_mm.to_i length = (point1[1] - point2[1]).abs.to_mm.to_i h = (point1[2] - point2[2]).abs.to_mm.to_i @beam.set_attribute "Beam Information", "1. Type of Concrete", "#{@cstrength}"

                Ok one problem now, I have selected active_model as either ComponentInstance or Group. When its a group I can apply the attributes with no problem! But when its a component I am not able to add the information. Is there a way I can add all this information to it? and Is there a way to apply the same attributes to all the ComponentInstances in the active_model?

                active_model IS the model NEVER anything else like a ComponentInstance or a Group.
                You must specify either the ComponentInstance or the Group, and then set_attributes for them.
                If you want to set_attributes for them, you must first specify them clearly...
                IF the first thing in a selection is a Group you can apply attributes to it.
                IF the first thing in a selection is a ComponentInstance you can apply attributes to it.
                [You can even get the instance.definition of it and set_attributes for that]
                I see no problems... except that you a not selecting the right type of thing to set_attributes to ?

                IF @beam = Sketchup.active_model.selection[0] IS the beam then it works... BUT is it the beam first thing in the selection ? You don't tell us enough to see what the @beam = Sketchup.active_model.selection[0] might be ! πŸ˜’

                TIG

                1 Reply Last reply Reply Quote 0
                • thomthomT Offline
                  thomthom
                  last edited by

                  apply the attibs to the definition.

                  Thomas Thomassen β€” SketchUp Monkey & Coding addict
                  List of my plugins and link to the CookieWare fund

                  1 Reply Last reply Reply Quote 0
                  • M Offline
                    meeta
                    last edited by

                    what exactly is a component definition?

                    Meet A

                    1 Reply Last reply Reply Quote 0
                    • M Offline
                      meeta
                      last edited by

                      	if (Sketchup.active_model.selection[0].typename == "Group" or Sketchup.active_model.selection[0].typename == "ComponentDefinition")
                      			menu.add_separator
                      			menu.add_item("Beam Analyser") {
                      	                b = Beam.new
                      			b.bbox
                      			b.prompts
                      .
                      .
                      .
                      .
                      .
                      .
                      
                      def bbox 
                      		@beam = Sketchup.active_model.selection[0]
                      		bbox = @beam.bounds
                      		point1 = bbox.max
                      		point2 = bbox.min
                      		b = (point1[0] - point2[0]).abs.to_mm.to_i
                      		length = (point1[1] - point2[1]).abs.to_mm.to_i
                      		h = (point1[2] - point2[2]).abs.to_mm.to_i	
                      end	
                      

                      does this make it more clear? If I choose a group, there is not problem. But if I choose a componentinstance, The attributes are applied without any problems. Unfortunately its not being applied to @beam.

                      Meet A

                      1 Reply Last reply Reply Quote 0
                      • thomthomT Offline
                        thomthom
                        last edited by

                        Would it be that your use of or instead of || makes this lines not evaluate correctly?
                        if (Sketchup.active_model.selection[0].typename == "Group" or Sketchup.active_model.selection[0].typename == "ComponentDefinition")

                        or has a lower precedence than ||
                        http://www.techotopia.com/index.php/Ruby_Operator_Precedence

                        Thomas Thomassen β€” SketchUp Monkey & Coding addict
                        List of my plugins and link to the CookieWare fund

                        1 Reply Last reply Reply Quote 0
                        • TIGT Offline
                          TIG Moderator
                          last edited by

                          Things [each called an Entity] that are 'visible' in a model are typically geometry [lines, faces etc], and groups and components [what is commonly called a component is actually a 'component-instance']. These groups and instances contain geometry - they can also contain sub-groups and other instances. This is a simplified picture since there's also Images, Text, Dimensions etc etc but lets keep it simple !
                          Within the model's 'database' there are other things you can't see but that you can use - inside 'collections' called Materials, Layers, Styles and ComponentDefinitions. Each of these will have entries like Material, Layer, Style and ComponentDefinition.

                          So far so good ?

                          When you look at the Components in the Browser you are looking into the model's database and seeing the Component-definitions available for use.

                          When you select [highlight] something in a model you can get its 'class' [typename etc] so with the initial examples above you have Sketchup::Edge, Sketchup::Face, Sketchup::Group and Sketchup::ComponentInstance.

                          Let's assume you have somehow set a variable instance=ss[i] which is a Sketchup::ComponentInstance.
                          You can find its definition thus definition=instance.definition

                          Now you can find an instance.definition but conversely you can find a definition.instances [i.e. all of that definition's instances]
                          Any particular definition's instance doesn't contain anything at all - it is a marker for the definition itself, so instance.definition.entities gives you a list of entities inside the definition that that instance is using...
                          You can even 'swap' an instance's definition for another one: instance.definition=another_definition ...

                          β˜€

                          TIG

                          1 Reply Last reply Reply Quote 0
                          • 1
                          • 2
                          • 1 / 2
                          • First post
                            Last post
                          Buy SketchPlus
                          Buy SUbD
                          Buy WrapR
                          Buy eBook
                          Buy Modelur
                          Buy Vertex Tools
                          Buy SketchCuisine
                          Buy FormFonts

                          Advertisement