I thought I'd just got to a working version of this tool, when I discovered a bug in my code that I don't understand, and wonder if anyone can help me figure it out.
I have drawn (using my Draw Framing tool) a component representing a rectangular piece of wood, at an arbitrary angle (though it can also happen at orthogonal angles).
I pick a point on its face from which to start drawing another component, but I find that the reported face.normal vector is along the green axis, not normal to the face.
The image may help to visualize it. The magenta rectangle is generated by the draw_geometry method of my tool, which also draws the face normal along what will become the @long_axis of the about-to-be-created component.
But as you can see in the drawing, instead of being normal to the face, the @long_axis is parallel to the green axis. (By the way, the 3x2 cursor text is the nominal size in inches of the cross section of the component about to be drawn, in case you were wondering.)
The code which finds the face normal is derived from the sample ruby CLineTool:
@ip1.pick view, x, y
if( @ip1.valid? )
... other code...
## Detect if pick point is on a face, and if so, orient long axis normal to it
# unless axis is locked
if @ip.face
f = @ip.face
puts "Face picked; normal is \n"
puts f.normal.inspect
if @@axis_lock == NO_LOCK # axis not locked
@long_axis = f.normal
puts "@long_axis = " + @long_axis.inspect
This outputs in the Ruby console :
Face picked; normal is
Vector3d(0, 1, 0)
@long_axis = Vector3d(0, 1, 0)
The code which draws the feedback geometry is:
# Display long axis as visual feedback
view.line_width = 2; view.line_stipple = ""
view.set_color_from_line(pt1 - @long_axis, pt1 + @long_axis)
view.draw_line(@first_pick.position, @first_pick.position + @long_axis)
...
view.drawing_color = "magenta"
view.draw_polyline(@profile_points)
I find that if I open the component for editing, and copy and paste its top face into the World coordinate system, the face.normal IS normal to this face, which is in the red-blue plane, not at the angle it appears as the top surface of the component.
I'm sure this is because I've originally drawn the far end face of the visible component in the r-g plane (as @profile_points0, which got transformed into @profile_points in the feedback view.draw.polyline above), then did the same transforms on the component face -- rotated it about two different axes and translated it -- then push-pulled it along (its) long_axis to generate the component shown.
Yet (as you can see from the blue highlighted bounding box) the previous component axes are in line with its faces.
In case it helps, I also attach the complete tool draw_framing.rb file.
Any clues as to how to fix this would be most welcome. Until I discovered this 'bug' in my code, I thought I was nearing v1.0 of my code!
So how do I reset the axes of the original face and the component which it becomes, to get a component drawn whose faces AREN'T weirdly distorted internally so as to throw off the apparent face.normal?
I found (before I recoded it this way) that if I first made a group, then a component instance, it didn't have this problem, but its component axes were along world axes, not tightly around the shape of a non-orthogonal component. That is easy to fix manually with a Change Axes command, but there isn't an API function to do the same, afaik.
If anyone knows how to do that, I could revert to that method of creating the component instance.
For example, would it help to explode the component in code, then re-group it and make component?
Or can anyone suggest an alternative approach altogether?
Thanks in advance for any advice I could follow, to work round this.
Code that generates this odd component