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

    Load Component and Rotation

    Scheduled Pinned Locked Moved Developers' Forum
    7 Posts 2 Posters 496 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.
    • pingpinkP Offline
      pingpink
      last edited by

      Hi ,

      I'm trying to load a sunshade component in " 4 sides of drawing edges " .
      

      I can do only one side. Anyone can give me any idea how can I load with a rotation in a code ?
      I want the code can apply around a building facade.

      
      require 'sketchup.rb'
      
         def insert_file_skp(sFile, point, sName)
       
         model = Sketchup.active_model
         pants_def = model.definitions.load sFile
         
         # Then define a location, and place our skp there.
         pants_location = point 
         transform = Geom;;Transformation.new pants_location
         entities = Sketchup.active_model.active_entities
         instance = entities.add_instance pants_def, transform
         instance.name= sName
         return instance
      end
      
      
      def move_compo(edge, compo)
        model = Sketchup.active_model
        start_point = edge.start.position
        end_point = edge.end.position
        if start_point.x < end_point.x 
          pt = start_point
        else
          pt = end_point
        end
        p pt
        
      
        ## Z Scale 90 degrees
         cpt = compo.transformation.origin 
        xscale = edge.length / 1.m
        tscale = Geom;;Transformation.scaling cpt, xscale, 1, 1
        compo.transform!(tscale) 
        
        
        vector = pt - cpt
        tr = Geom;;Transformation.translation vector
        compo.transform!(tr)
        
      end
      
      
      def insert_Sunshade_Curved
         model = Sketchup.active_model 
         model.start_operation( "Add Component", true)
         selection = model.selection 
         selection.each{|entity|
         sFile = Sketchup.find_support_file "CurtainWall_Architect/Components/Sunshade/Sunshade_Curved.skp", "Plugins"
      	  	
           if entity.class == Sketchup;;Edge
      	    edge = entity
      		line = edge.line #vector of edge
      		if (line[1].dot Z_AXIS).abs == 0.0 
      		compo = insert_file_skp( sFile, Geom;;Point3d.new(0,0,0), "Sunshade-Curved")
      	        move_compo(edge, compo)			
           end 
         
         } 
      
         model.commit_operation 
      
      end
      

      Sunshade_1.png


      Sunshade_2.png


      Sunshade_4.jpg

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

        Your code inserts an instance at the origin, scales it to the edge.length and moved it to the edge.start.position.

        So far so good.

        Now in move_compo() you need to find the angle that the edge makes with the X_AXIS.

        I am typing this from memory, as I can't test it right now, so please be aware that you might need to spot typos and even flip some logic***

        vec = edge.line[1] ang = X_AXIS.angle_between(vec) ang = -ang if X_AXIS.cross(vec).z < 0***

        ***e.g. it might be > 0 ?

        Now rotate the instance about the edge.start.position

        trr = Geom::Transformation.rotation(edge.start.position, Z_AXIS, ang) compo.transform!(trr)

        That should rotate the instance onto its edge's line...

        TIG

        1 Reply Last reply Reply Quote 0
        • pingpinkP Offline
          pingpink
          last edited by

          Thank you so much !
          I try to solve form your suggestion It's rotate , but not place at the edges.
          I don't know why.


          require 'sketchup.rb'

          def insert_file_skp(sFile, point, sName)

          model = Sketchup.active_model
          pants_def = model.definitions.load sFile

          Then define a location, and place our skp there.

          pants_location = point
          transform = Geom::Transformation.new pants_location
          entities = Sketchup.active_model.active_entities
          instance = entities.add_instance pants_def, transform
          instance.name= sName
          return instance
          end

          def move_compo(edge, compo)
          model = Sketchup.active_model
          start_point = edge.start.position
          end_point = edge.end.position

          #------------- Edit ----------------#
          [highlight=#ffff80:1fxmkzqh]vec = edge.line[1]
          ang = X_AXIS.angle_between(vec)
          ang = -ang if X_AXIS.cross(vec).z < 0[/highlight:1fxmkzqh]
          #------------------------------------#
          if start_point.x < end_point.x
          pt = start_point
          else
          pt = end_point
          end
          p pt

          cpt = compo.transformation.origin

          xscale = edge.length / 1.m
          tscale = Geom::Transformation.scaling cpt, xscale, 1, 1
          compo.transform!(tscale)

          vec = pt - cpt

          #------------- Edit ----------------#
          [highlight=#ffff80:1fxmkzqh]tr1 = Geom::Transformation.translation vec
          tr2 = Geom::Transformation.rotation(edge.start.position, Z_AXIS, ang)

          instance1 = compo.transform!(tr1)
          instance2 = compo.transform!(tr2)[/highlight:1fxmkzqh]

          #------------------------------------#

          end

          def insert_Sunshade_Curved
          model = Sketchup.active_model
          model.start_operation( "Add Component", true)
          selection = model.selection
          selection.each{|entity|
          sFile = Sketchup.find_support_file "CurtainWall_Architect/Components/Sunshade/Sunshade_Curved.skp", "Plugins"

           if entity.class == Sketchup::Edge
              edge = entity
          	line = edge.line #vector of edge
          		
          	if (line[1].dot Z_AXIS).abs == 0.0 
          		compo = insert_file_skp( sFile, Geom::Point3d.new(0,0,0), "Sunshade-Curved")
                 move_compo(edge, compo)
           
          	end   
          
           end 
          

          }

          model.commit_operation

          end


          Result

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

            You want to move the compo that you have initially placed at the ORIGIN to the start of the edge ?
            So use
            vect = ORIGIN.vector_to(edge.start.position)
            Then use that to move it into place [and then do my rotation code...]
            tr1 = Geom::Transformation.translation(vect) tr2 = Geom::Transformation.rotation(edge.start.position, Z_AXIS, ang) compo.transform!(tr1) compo.transform!(tr2)

            Check my ..z < 0 works... it might need to be ..z > 0 ??

            I haven't managed to test any of this πŸ˜•

            Incidentally, you can combine transformations thus
            compo.transform!(tr2*tr1)
            once you know it's working right - note the order of the combination * - relocating to a point then rotating about that point, is likely to produce a different result compared to rotating about a point then relocating to that point...

            TIG

            1 Reply Last reply Reply Quote 0
            • pingpinkP Offline
              pingpink
              last edited by

              Thank you very much , TIG 😍

              Finally I can load , scale , and rotate sunshades from your advice !!!

              This code works very nice. z < 0 made the components run on the edges.


              def move_compo(edge, compo)
              model = Sketchup.active_model
              start_point = edge.start.position
              end_point = edge.end.position

              #------------- Edit ----------------#

              [highlight=#ffff80:1zu9kjw1]vect = edge.line[1]
              ang = X_AXIS.angle_between(vect)
              ang = -ang if X_AXIS.cross(vect).z < 0[/highlight:1zu9kjw1]
              #------------------------------------#
              cpt = compo.transformation.origin

              xscale = edge.length / 1.m
              tscale = Geom::Transformation.scaling cpt, xscale, 1, 1
              compo.transform!(tscale)

              vect = ORIGIN.vector_to(edge.start.position)

              #------------- Edit ----------------#

              [highlight=#ffff80:1zu9kjw1]tr1 = Geom::Transformation.translation(vect)
              tr2 = Geom::Transformation.rotation(edge.start.position, Z_AXIS, ang)
              compo.transform!(tr2*tr1)[/highlight:1zu9kjw1]

              #------------------------------------#

              end


              Sunshade_Rotate_1.jpg

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

                Glad to help.
                I guessed at the right 'cross' result - it'd either be .z<0 or .z>0 !
                πŸ˜„

                TIG

                1 Reply Last reply Reply Quote 0
                • pingpinkP Offline
                  pingpink
                  last edited by

                  I respect you !!! I'm a big fan of you. πŸ˜„

                  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