Help with transformation
-
@unknownuser said:
My task is to place a component and scale it's x axis, between two points.
OK.. gotcha.
You also need a y-axis vector
def place_and_tweek( defn, pt1, pt2 ) xvec = pt1.vector_to( pt2 ) yvec = xvec.axes[1] # ??? tp = Geom;;Transformation.new( pt1 ) inst = ents.add_instance( defn, tp ) ts = Geom;;Transformation.scaling( xvec.length, 1.0, 1.0 ) inst.transform!( ts ) ta = Geom;;Transformation.new( pt1, xvec, yvec ) inst.transform!( ta ) end
-
OK - I finished I think.
This is what came out:def add_Filament( startPoint, endPoint , filament ) @lineVector = startPoint.vector_to endPoint return if @lineVector.length.to_f - 1e-6 < 0.0 # Scaling and adding an instance of the filament @scale = @lineVector.length.to_f / filament.bounds.height #width #depth @trans = Geom;;Transformation.scaling 1, @scale, 1 @instance = Sketchup.active_model.active_entities.add_instance( filament, @trans ) # if needed rotate the filament @lineVector.normalize! @instYaxis = Geom;;Vector3d.new( 0, filament.bounds.height, 0 ).normalize #height givs the Y axis @rotDeg = @instYaxis.dot( @lineVector ) @rotDeg = Math;;acos( @rotDeg ) if @rotDeg.to_f - 1e-6 > 0.0 if @rotDeg.to_f + 1e-6 > Math;;PI @rotVector = Geom;;Vector3d.new( 0, 0, 1 ) else # if PI @rotVector = @instYaxis.cross( @lineVector ) end # if PI @trans = Geom;;Transformation.rotation( ORIGIN, @rotVector, @rotDeg ) @instance.transform!( @trans ) end # if @rotDeg > 0.0 # Moving the filament to the startPoint if not ( ORIGIN == startPoint ) @trans = Geom;;Transformation.new startPoint @instance.transform!( @trans ) end #if not ORIGEN end
-
David, You did a bit more work than was required. The entire section regarding rotation is not needed. You only need to add ", @lineVector" to the last Transformation
def add_Filament( startPoint, endPoint , filament ) @lineVector = startPoint.vector_to endPoint return if @lineVector.length.to_f - 1e-6 < 0.0 # Scaling and adding an instance of the filament @scale = @lineVector.length.to_f / filament.bounds.height #width #depth @trans = Geom;;Transformation.scaling 1, @scale, 1 @instance = Sketchup.active_model.active_entities.add_instance( filament, @trans ) # Moving the filament to the startPoint if not ( ORIGIN == startPoint ) @trans = Geom;;Transformation.new startPoint, @lineVector @instance.transform!( @trans ) end #if not ORIGEN end
-
Do I need to do the checking ORIGIN etc..?
Or it should be:def add_Filament( startPoint, endPoint , filament ) @lineVector = startPoint.vector_to endPoint return if @lineVector.length.to_f - 1e-6 < 0.0 # Scaling and adding an instance of the filament @scale = @lineVector.length.to_f / filament.bounds.height #width #depth @trans = Geom;;Transformation.scaling 1, @scale, 1 @instance = Sketchup.active_model.active_entities.add_instance( filament, @trans ) # Moving And Rotating the filament to the @lineVector @trans = Geom;;Transformation.new startPoint, @lineVector @instance.transform!( @trans ) end # def add filament
P.S - Why filament.bounds.height is the length of the Y axis of the component?
I thought height is the Z axis - no? -
No you don't need the ORIGIN check. Since you don't specify a location when adding the instance, it is by default placed at the origin.
I agree, you would think height would be Z but no, bounds.width=X,bounds.height=Y, and bounds.depth=Z.
Doing the scaling when adding the instance is something I had never thought of so I learned something too.
-
hhh - doesnt work
All our work for nothing -
@davidfi1 said:
hhh - doesnt work
All our work for nothingWhy doesn't it work? How is it failing?
Provide screen shots or sample model.
How the component is made and the axes defined will always determine how it orients when placed.
-
Well, In the documentation it says that if the second parameter is a vector
It will be used as a Z axis - this is what is happenening!.Quote:
Geom::Transformation.new(origin, zaxis) creates a Transformation where origin is the new origin, and zaxis is the z axis. The x and y axes are determined using an arbitrary axis rule. -
@davidfi1 said:
Well, In the documentation it says that if the second parameter is a vector
It will be used as a Z axis - this is what is happenening!.Quote:
Geom::Transformation.new(origin, zaxis) creates a Transformation where origin is the new origin, and zaxis is the z axis. The x and y axes are determined using an arbitrary axis rule.That's why I mentioned how the orientation of the component when created would be critical. If you could post a model with the component in it would make it much easier to figure out what you need to do to correct the problem. Just a wild guess but try this
# Moving the filament to the startPoint xa,ya,za=@lineVector.axes @trans = Geom;;Transformation.new xa,za,ya,startPoint @instance.transform!( @trans )
-
Thank you.
I will post the G-Code importer I am workin on soon
Then you could play with it and see if it is possible to make the original code more elegant.
The code I posted works!. It is a little ... well ... not very elegant - but it works -
Posted the first vertion of the importer at
http://sketchucation.com/forums/viewtopic.php?f=323&t=50067
Advertisement