Planes
-
One click on a face can give you that face.
From that face you can get face's 'normal' - the vector perpendicular to the face.
As you can already make you grid-squares 'flat' - where its 'normal' vector is the Z_AXIS [0,0,1] - you can work out the angle of rotation between that and the face's normal thus:
angle=Z_AXIS.angle_between(face.normal)
You can now make all of your grid-squares inside a [temporary?] group, then when that's done transform that group by the rotation angle [I've skipped translating it into place here]:
assumingpoint
is where you want the rotation to be anchored:
perpvector=Z_AXIS.cross(face.normal)
### a vector perpendicular to the other two...
tr=Geom::Transformation.rotation(point,perpvector,angle) if perpvector.length!=0
### this leaves it as it is if it's flat as well...Hope this helps...
-
Thanks very much for the advice Tig,
Reassured that I can rotate the plane I've been finishing up the code to draw from a start point to a finish point in 2d (I'd been holding off worried I'd have to undo it all to fiddle in some trig).
I need each square to be moveable and deletable so I've set about grouping things:
group = entities.add_group
group.entities.add_face pt1, pt2, pt3, pt4
This is within the loop so every spot (square) groups.
Now my problem:
I then need to add all of these groups to a master group temporarily to rotate and translate as you previously described.
How do I do this? I'm a bit stuck, tried all sorts of combos of group.entities... but can't figure the right syntax. Online help files don't enlighten me much eitheraside from that, grabbing the 3d vector of the face seems to have gone well:
face = @ip1.face
puts "facenormal " + face.normal.to_s
rotationangle = Z_AXIS.angle_between(face.normal)
puts "rotationangle " + rotationangle.to_sSo thanks with that bit.
Once I get this group issue solved then I'll see how the rest of it (rotation etc.) goes.Also a quick question, my computer is seriously slow to draw this. I'm sure my code is inefficient, do you think I'd gain much by drawing a 4 lines, grouping them, then copy paste and translating them each time rather than redrawing and regrouping. Its so tedious I've had to add:
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
endI'm sure you can see from my code how unfamiliar I am with this ruby malarkey trying to struggle on
-
it doesn't seem to work
whenever I leave in the:
master_group=model.add_group()
line then my program get confused and starts reporting errors. I had set up error handling for click and drag rather than click move click, so it will only accept click move click. This code was lifted straight from the line tool example that comes with sketchup and was working fine before so is unlikely to be the cause.
When I look online at the model tag (http://code.google.com/apis/sketchup/docs/ourdoc/model.html) it makes no mention of the add_group command.
I've tried substituting entities for model, i.e. ... = entities.add_group() and even ... = model.entities.add_group, but neither of these work. They'll group individual spots but not the whole lot. What am I missingThanks for persevering
-
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 helpI'll see how much further I can get now...
-
Sorry - stupid typo
master_group=model.add_group()
should read
master_group=entities.add_group()
I'll correct original code too... -
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...
-
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!=0before 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 -
You define a transformation, then you apply it to an object.
There are several types of transformation for placing, translating, rotating, scale/mirroring etcSo
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 method90.degrees
instead of the equivalentMath::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 theface.normal
is the vector around which the rotation is made - the angle is arbitrary.
Theif
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
Thecross
method returns a vector at right-angles to the two vectors - useful for a rotations like you want... theif...length...not...
test is to check that theperpvector
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?]... -
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 -
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:intransform!' 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! trafter 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? -
You make a transformation and apply it to the group.
...So...
define or getpoint
,vector
andangle
...then...
tr=Geom::Transformation.rotation(point,vector,angle)
...then...
master_group.transform!(tr)
The!
at the end of thetransform
method changes the original object iftr
is a transformation.
If you addputs 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.gpoint=[], 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...
-
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:intransform!' 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? -
Add these lines after the transformation '
tr
' is supposedly made
puts tr.class return nil
The Ruby Console should shown whattr
is - don't mess on with to_s
etc.
It should just say it's aGeom::Transformation
- nothing else...
Thereturn 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 calledtrans
and something calledtr
here can you publish the part of the code so we can see what is going wrong ?
What istrans
?
Are you somehow putting stuff into an array (==[]) in error ? -
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
-
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 thattransform!(tr)
always has something to work on ? -
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
-
PM me the ruby and I'll try and look at it later today...
-
I think the problem is that you define tr in a seperate place than you use it. try replacing tr with @tr.
-
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.
Advertisement