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

    Planes

    Scheduled Pinned Locked Moved Developers' Forum
    23 Posts 5 Posters 726 Views 5 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.
    • L Offline
      lasersage
      last edited by

      sweet I fixed it already,
      it was supposed to be entities.add_group not model.add_group, but it was a combination of that and having it within the loop when it only needed to be defined once.
      Awesome πŸ˜„
      Thanks for the help

      I'll see how much further I can get now...

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

        Sorry - stupid typo
        master_group=model.add_group()
        should read
        master_group=entities.add_group()
        😳
        I'll correct original code too...

        TIG

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

          To add a group inside a group...

          model=Sketchup.active_model
          entities=model.active_entities
          master_group=entities.add_group()
          ### ...
          group=master_group.entities.add_group()
          face=group.entities.add_face(p0,p1,p2,p3)
          ### etc
          
          

          you now have a group containing a face inside a master_group...
          By iterating through a list you can add lots of separate groups to the master_group...
          So make your master_group first and add to its entities - don't make loads of separate groups and then decide to group them...
          When you've made all of you sub-groups transform the master_group as desired...
          πŸ€“

          TIG

          1 Reply Last reply Reply Quote 0
          • L Offline
            lasersage
            last edited by

            No worries Tig, the odd typo is bound to happen. Makes me feel more confident that I can work out the odd thing as well.

            So I've pasted in this stuff
            face = @ip1.face
            puts "facenormal " + face.normal.to_s
            rotationangle = Z_AXIS.angle_between(face.normal)
            perpvector = Z_AXIS.cross(face.normal)
            tr = Geom::Transformation.rotation(@ip1,perpvector,angle) if perpvector.length!=0

            before the loop, as "face" gets used for other stuff in the loop, and as you can see, changed point to @ip1 (the first click of the mouse). Hope that is a reasonable point to assume? I get that some significant translation might be required afterwards, but hopefully it'll still turn the plane to the right angle?

            But the
            tr = Geom::Transformation.rotation(@ip1,perpvector,angle) if perpvector.length!=0
            bit of code. I don't really get it. Presumably you can only rotate a group once you've already made it. I've tried cutting just this line and sticking it after the groups are formed, just after the loop, but it goes back to click and drag error.
            This line of code makes no mention of "master_group" so I don't really get how it works anyway.
            Do I need to call something like master_group.rotation = tr ?
            or did I misread your first comment on this thread and this line of code is purely to zero the plane and I actually need an entirely different line of code for the actual rotation.
            Sorry to be such a newb on this one πŸ˜•

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

              You define a transformation, then you apply it to an object.
              There are several types of transformation for placing, translating, rotating, scale/mirroring etc

              So
              tr=Geom::Transformation.rotation(point, vector, angle)
              sets up a rotation transformation, then
              master_group.transform!(tr)
              transforms the master_group using 'tr'...
              'point' is the Point3d you want as the rotation's anchor.
              'vector' is the vector around which the rotation will take place.
              'angle' is the angle in radians - if you have calculated it [as an angle_between vectors for example] it's already in radians - if you are typing it there is a user friendly method 90.degrees instead of the equivalent Math::PI/2...
              So to rotate it 90 degrees around the z-axis anchored at the origin it'd be
              tr=Geom::Transformation.rotation(ORIGIN, Z_AXIS, 90.degrees)
              or
              tr=Geom::Transformation.rotation([0,0,0], [0,0,1], 90.degrees)
              If you are rotating something 'on' a face the point is on the face and the face.normal is the vector around which the rotation is made - the angle is arbitrary.
              The if test I added was to check if the object needed rotation...
              rotationangle = Z_AXIS.angle_between(face.normal) perpvector = Z_AXIS.cross(face.normal) tr = Geom::Transformation.rotation(@ip1,perpvector,angle) if perpvector.length!=0
              The cross method returns a vector at right-angles to the two vectors - useful for a rotations like you want... the if...length...not... test is to check that the perpvector is valid - i.e. if the 'cross' fails because the two vectors 'crossed' don't have a right-angled result [i.e. no rotation is needed?]... ❓

              TIG

              1 Reply Last reply Reply Quote 0
              • L Offline
                lasersage
                last edited by

                looks good thanks Tig.
                I'm off for the weekend now, but I've just sent all the files to my home email so I'll see if I can find time to have a bash at it in my own time.
                I'll let you know how it goes on monday.
                Thanks again for the help so far, I'd be stuck without it

                1 Reply Last reply Reply Quote 0
                • L Offline
                  lasersage
                  last edited by

                  OK I've had a look at it but not really got anywhere 😞
                  I've tried both:
                  tr=Geom::Transformation.rotation(point, vector, angle)
                  tr=Geom::Transformation.rotation(ORIGIN, Z_AXIS, 90.degrees)

                  just to make sure there wasn't a problem here, and had
                  puts "transformation " + tr.to_s
                  put what looks like a reasonable output into the console.
                  All OK, but whenever I run the program it bugs out to the old click drag click error message and the console shows:

                  Error: #<TypeError: no implicit conversion to Transformation>
                  C:/Program Files/Google/Google SketchUp 7/Plugins/test5.rb:117:in transform!' C:/Program Files/Google/Google SketchUp 7/Plugins/test5.rb:117:in onLButtonDown'

                  So what it wrong with my transform! bit?
                  I've tried it as:
                  master_group = master_group.transform!(tr)
                  &
                  master_group.transform! tr

                  after the loop and before it (straight after master_group is defined). None of it works, it always gives me this daft conversion message. But I'm convinced the first bit of code does give a valid transformation, so why does it then think it the wrong type or something?
                  It can't just be down to where I'm placing the
                  master_group.transform! tr
                  line in the code can it?

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

                    You make a transformation and apply it to the group.
                    ...So...
                    define or get point, vector and angle
                    ...then...
                    tr=Geom::Transformation.rotation(point,vector,angle)
                    ...then...
                    master_group.transform!(tr)
                    The ! at the end of the transform method changes the original object if tr is a transformation.
                    If you add puts tr.class what do you get - it looks like it's not a real transformation to me ...
                    The second one with the values set shouldwork OK ?
                    Pre-set the variables you use outside of all of your 'do loops' or {} blocks - e.g point=[], vector=[], angle=0.0, tr=nil etc near the start of the script...
                    It might be that they are not getting transfered between the loops - setting them first means they will be...
                    β˜€

                    TIG

                    1 Reply Last reply Reply Quote 0
                    • L Offline
                      lasersage
                      last edited by

                      when I added the =0.0, =nil etc. line of code nothing changed,
                      and when I tried "trans class " + tr.class
                      it gave me a cannot be displayed as string message, so I did tr.class.to_s and that gave me:
                      point (503.690114mm, 371.165908mm, 943.266108mm)
                      angle 1.5707963267949
                      perpvector (1.0, -6.27276008913213e-015, 0.0)
                      transformation #Geom::Transformation:0xba74610
                      trans class Geom::Transformation
                      Error: #<TypeError: no implicit conversion to Transformation>
                      C:/Program Files/Google/Google SketchUp 7/Plugins/test5.rb:170:in transform!' C:/Program Files/Google/Google SketchUp 7/Plugins/test5.rb:170:in onLButtonDown'

                      So it looks like the transformation is OK, but it doesn't like it when it calls transform! .
                      My geom::transformation line is just after the first click but before the loop, my transform! line is after the loops.
                      Any ideas?

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

                        Add these lines after the transformation ' tr' is supposedly made
                        puts tr.class return nil
                        The Ruby Console should shown what tr is - don't mess on with t o_s etc.
                        It should just say it's a Geom::Transformation - nothing else...
                        The return nil just stops execution at that point so you don't have to wait for a later failure...
                        You seem to be messing up something called trans and something called tr here πŸ˜• can you publish the part of the code so we can see what is going wrong ?
                        What is trans ?
                        Are you somehow putting stuff into an array (==[]) in error ?

                        TIG

                        1 Reply Last reply Reply Quote 0
                        • L Offline
                          lasersage
                          last edited by

                          Here's the code in full, I'd only held off posting it till now as I was keen to learn enough to fix it myself, but as this is getting frustrating I'll welcome any solutions πŸ˜„

                          the initialization of the variables is hashed out because I changed it to initialize in the def initialize sub, rather than here. Not sure if that's good or not though as they might need to be "@point" to carry the value over.

                          Hope you can decipher it with all the wordwraping, I tend to leave long lines and comments, I was going to tidy up the defunct code hashed out when I finally got it working.

                          
                          def onLButtonDown(flags, x, y, view)
                              # When the user clicks the first time, we switch to getting the
                              # second point.  When they click a second time we create the line
                          	
                          		#point=[], perpvector=[], angle=0.0, tr=nil
                              if( @state == 0 )
                                  @ip1.pick view, x, y
                                  if( @ip1.valid? )
                                      @state = 1
                                      Sketchup;;set_status_text $exStrings.GetString("Select second point (top right)"), SB_PROMPT
                                      @xdown = x
                                      @ydown = y
                          			face = @ip1.face
                          			puts "facenormal " + face.normal.to_s
                          			angle = Z_AXIS.angle_between(face.normal)
                          			perpvector = Z_AXIS.cross(face.normal)
                          			point = @ip1.position
                          			tr = Geom;;Transformation.rotation(point,perpvector,angle) if perpvector.length!=0
                          			#tr=Geom;;Transformation.rotation(ORIGIN, Z_AXIS, 90.degrees)
                          			puts "point " + point.to_s, "angle " + angle.to_s, "perpvector " + perpvector.to_s, "transformation " + tr.to_s, "trans class " + tr.class.to_s, tr.class
                          			
                          			if face == nil
                          				UI.messagebox "The points must be on the face of an object"
                          				self.reset
                          			end
                                  end
                              else
                                  # create the line on the second click
                                  if( @ip2.valid? )
                                      self.create_geometry(@ip1.position, @ip2.position,view)
                          			@ip2.pick view, x, y
                          			
                          				model = Sketchup.active_model
                          				entities = model.entities
                          				master_group = entities.add_group()
                          				#master_group = master_group.transform!(tr)
                          				
                          				numsquaresX = 10 	#number of squares in the X direction
                          				xsize = 30			#size of squares in the X direction (mm)
                          				xsize = xsize/25.4  #converts xsize into mm (from inches)
                          				numsquaresY = 10	#number of squares in the Y direction
                          				#ysize = 3			#size of squares in the Y direction
                          				#ysize = ysize/25.4 #converts ysize into mm (from inches)
                          				ysize = xsize		#as we're generally only dealing with squares, edit this line out and reinstate the two above lines for rectangles
                          				zsize = xsize
                          				z = 0
                          				
                          				x1 = @ip1.position.x
                          				y1 = @ip1.position.y
                          				z1 = @ip1.position.z
                          				
                          				xfinal = @ip2.position.x
                          				yfinal = @ip2.position.y
                          				zfinal = @ip2.position.z
                          				
                          				#puts x1, y1, z1, xfinal, yfinal, zfinal
                          				if (xfinal - x1) > (25 * xsize)
                          					largetest = UI.messagebox "You have selected a large area, this may make the program unstable. Suggest multiple smaller areas", MB_OKCANCEL
                          					if largetest == 2
                          						self.reset
                          					end
                          				end
                          
                          				while y1 < yfinal
                          					y2 = y1 + ysize
                          					while x1 < xfinal
                          						x2 = x1 + xsize
                          						pt1 = [x1, y1, z]									#plot the first line
                          						pt2 = [x1, y2, z]									#plot the second line
                          						pt3 = [x2, y2, z]									#plot the third line
                          						pt4 = [x2, y1, z]									#plot the fourth line, back to start
                          						#new_face = entities.add_face pt1, pt2, pt3, pt4	#plot a face on the square to make it visible
                          						#group.entities.add_face pt1, pt2, pt3, pt4
                          
                          						group = master_group.entities.add_group
                          						face = group.entities.add_face pt1, pt2, pt3, pt4
                          												
                          						x1 = x2 - (xsize * 0.08) #let the next spot begin at 92% of the way across the previous
                          						Sketchup;;set_status_text $exStrings.GetString("Working"), SB_PROMPT
                          						#puts "Point1 " + pt1.to_s, "Point2 " + pt2.to_s, "Point3 " + pt3.to_s, "Point4 " + pt4.to_s
                          					end
                          					y1 = y2 - (ysize * 0.08) #let the next spot begin at 92% of the way across the previous
                          					#x1 = x1 - (xsize * 0.92 * xstep) #go back to initial x position for next line in y
                          					x1 = @ip1.position.x
                          				end
                          			master_group.transform! tr
                          			self.reset(view)
                                  end #end click 2
                              end
                              
                              # Clear any inference lock
                              view.lock_inference
                          end
                          

                          let me know if you want me to send the complete .rb files, as this chunk of code wont run without the other def's around it

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

                            Looks like you need to have tr defined no matter what e.g.
                            tr=Geom::Transformation.rotation(ORIGIN, Z_AXIS, 0.degrees) if not perpvector
                            so that transform!(tr) always has something to work on ?

                            TIG

                            1 Reply Last reply Reply Quote 0
                            • L Offline
                              lasersage
                              last edited by

                              Well I added that line:
                              tr=Geom::Transformation.rotation(ORIGIN, Z_AXIS, 0.degrees) if not perpvector
                              right next to the other tr = Geom::..... line but its still doing the same thing.
                              Do you think it'd help if I just sent you the full .rb file so you could where it runs too?

                              It always seems to give the class as geom::transformation in the console, but whenever I uncomment the transform! line then it has trouble 😞

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

                                PM me the ruby and I'll try and look at it later today... πŸ˜•

                                TIG

                                1 Reply Last reply Reply Quote 0
                                • C Offline
                                  cjthompson
                                  last edited by

                                  I think the problem is that you define tr in a seperate place than you use it. try replacing tr with @tr.

                                  1 Reply Last reply Reply Quote 0
                                  • L Offline
                                    lasersage
                                    last edited by

                                    Well I thought that was a long shot as it was defined and used in the same def, but I'd forgotten about the initialize def. Sure enough @tr has got it!!!!
                                    Thanks very much.

                                    So its rotating and displaying it just as you'd expect, off floating in space to the side.
                                    I'll have a go at the translation code and see how that goes.

                                    Thanks very much guys, really pleased this is coming together.

                                    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