• Login
sketchucation logo sketchucation
  • Login
ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

Replace component and set dynamic attributes?

Scheduled Pinned Locked Moved Developers' Forum
3 Posts 2 Posters 750 Views 2 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.
  • S Offline
    skavan
    last edited by 16 Jan 2015, 02:31

    OK - here's the challenge.
    I have a model with several non-dynamic components (doors).
    I have a dynamic component (door) called LDOOR.

    I can select all the non-dynamic components -- and then what I want to do, for each such instance is:

    1. grab the height, width, depth and X,Y,Z, and rotation of the non-dynamic component
    2. assign height, width and depth and X, Y, Z and rotation to attributes: LenX, LenY and LenZ,X,Y,Z,RotX etc.. of the dynamic component.
    3. have the dynamic component update itself (i.e. react to the changes in #2)
    4. replace the non-dynamic component with the new instance of the dynamic component.

    It MUST be possible...and I have carefully studied TIG's component replacer code - but try as I might I can't get it to work.

    So far, I can easily select the dynamic component:
    model = Sketchup.active_model definitions = model.definitions component = definitions['LDOOR']

    and I can happily iterate through my selected "non dynamic components":
    sel=model.selection for instance in sel h= instance.bounds.height d= instance.bounds.depth w= instance.bounds.width x= instance.transformation.origin.x y= instance.transformation.origin.y z= instance.transformation.origin.z r= Math.asin(instance.transformation.to_a[4]).radians.round next

    But how to now create a new instance of the dynamic component and fill in the values!!!! H-E-L-P!

    1 Reply Last reply Reply Quote 0
    • S Offline
      skavan
      last edited by 16 Jan 2015, 18:29

      OK - getting close -- very close -- but stuck on getting the right rotation (and I'm sure there's a cleaner way of doing all this):

      The issue is that I'm retrieving the wrong rotation. If I create a rotZ attribute on the entity I'm trying to replace, it shows (for example) 135 degrees. But the instance.transformation array gives me a z-rotation of -45. I'm stumped! I've attached a sample scene. The dynamic door is RED and my targets are, well, not.

      door test scene.skp

      ` model = Sketchup.active_model
      entities = Sketchup.active_model.entities
      definitions = model.definitions
      component = definitions['LDOOR']

      model.start_operation("Create Door")
      #create a new instance
      transformation = Geom::Transformation.new([0,0,0])
      componentinstance = entities.add_instance(component, transformation)
      puts 'we have added a new instance (GUID:' + componentinstance.guid + ')'
      $dc_observers.get_latest_class.redraw(componentinstance)
      model.commit_operation

      model.start_operation("XForm Door")
      sel=model.selection
      instance = sel[0]

      x= instance.transformation.origin.x
      y= instance.transformation.origin.y
      z= instance.transformation.origin.z
      
      d= instance.definition.bounds.height
      h= instance.definition.bounds.depth
      w= instance.definition.bounds.width
      r= Math.asin(instance.transformation.to_a[4]).radians.round
      
      componentinstance.transformation = Geom::Transformation.new([x,y,z])
      
      d=2 ;#override depth
      
      #instance.attribute_dictionaries["dynamic_attributes"].each_pair{|k,v|p k;p v;puts}
      ad = componentinstance.attribute_dictionary "dynamic_attributes"
      ad["_lenx_formula"]=w.inspect
      	
      ad["_leny_formula"]=d.inspect
      
      ad["_lenz_formula"]=h.inspect
      
      ad["_rotz_formula"]=r.inspect
      
      $dc_observers.get_latest_class.redraw(componentinstance)
      entities.erase_entities instance
      

      model.commit_operation
      #`

      1 Reply Last reply Reply Quote 0
      • G Offline
        Garry K
        last edited by 24 Jan 2015, 16:50

        There are a few things that I had to deal with.

        I found it easier to put the dynamic attributes in a nested group as opposed to the outside group. This way you can rotate the entire door and jamb to fit it into the door opening and you don't have to keep changing the dynamic rotz. These doors can be copied, flipped and or rotated while preserving the desired angles of rotation.


        4 doors.skp


        4 doors.PNG

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

        Advertisement