Sketch orientation - align with selected face

Sketch orientation - align with selected face

NachoShaw
Advisor Advisor
1,386 Views
14 Replies
Message 1 of 15

Sketch orientation - align with selected face

NachoShaw
Advisor
Advisor

Hey

 

Im adding a sketch by doing the following-

 

select edge

add plane (normal to edge)

add workpoint

add sketch to plane on workpoint

add sketch lines to sketch at defined size (.75 x .25)

add constraints to sketch lines

 

problem im having is the orientation of the sketch. sometimes it correct, sometimes its not. I think id like to select a defining face first to set the direction of the sketch but im struggling to do that. 

edges.jpg

 

in the picture above, sketch (A) is how i need to be every time. Id like to select a face, align the sketch as shown and then it always orients this direction. Sketch (B) is rotated 90 degrees but needs to align with the selected face.

The pink lines i added to show the edges that i selected.

 

Is there a way to orient a sketch based on a selected face? here is my code:

Sub Rebate()

Dim oApp As Inventor.Application: Set oApp = ThisApplication
Dim oPartDoc As PartDocument: Set oPartDoc = oApp.ActiveDocument
Dim oCompDef As PartComponentDefinition: Set oCompDef = oPartDoc.ComponentDefinition

Dim oTG As TransientGeometry: Set oTG = oApp.TransientGeometry

Dim oFace As Face: Set oFace = oApp.CommandManager.Pick(kAllPlanarEntities, "pick rebate face")

Dim oEdge As Edge: Set oEdge = oApp.CommandManager.Pick(kPartEdgeFilter, "pick edge")

'Try to find the selected edge in the face edge collection
'if found, set the edge as the face edge
Dim e As Edge
For Each e In oFace.Edges
    If e.TransientKey = oEdge.TransientKey Then
        Set oEdge = e
    End If
Next

' Get the start vertex of the edge to use as the origin of the sketch.
Dim oVertex As Vertex
Set oVertex = oEdge.StartVertex

Dim oPT As WorkPoint: Set oPT = oCompDef.WorkPoints.AddByMidPoint(oEdge)
oPT.Visible = False
Dim oWP As WorkPlane: Set oWP = oCompDef.WorkPlanes.AddByNormalToCurve(oEdge, oPT)
oWP.Visible = False

''create the sketch
'Dim oSketch As PlanarSketch: Set oSketch = oCompDef.Sketches.Add(oWP)

Dim oSketch As PlanarSketch
Set oSketch = oCompDef.Sketches.AddWithOrientation(oWP, oEdge, True, True, oEdge.StartVertex, True)
'Set oSketch = oCompDef.Sketches.AddWithOrientation(oWP, oEdge, False, False, oVertex, False)


'project the edge to create a point
Dim p As SketchPoint
Set p = oSketch.AddByProjectingEntity(oEdge)

'create the rectangle at the projected point
Dim r As SketchEntitiesEnumerator
Set r = oSketch.SketchLines.AddAsTwoPointCenteredRectangle(p, oTG.CreatePoint2d(0.25, 0.125))

Dim oSketchLine As SketchLine
Set oSketchLine = r.Item(1)
Call oSketch.DimensionConstraints.AddTwoPointDistance(oSketchLine.StartSketchPoint, oSketchLine.EndSketchPoint, kHorizontalDim, oTG.CreatePoint2d(0, -2))

Set oSketchLine = r.Item(4)
Call oSketch.DimensionConstraints.AddTwoPointDistance(oSketchLine.StartSketchPoint, oSketchLine.EndSketchPoint, kVerticalDim, oTG.CreatePoint2d(-2, 0))

oSketch.AxisEntity = oCompDef.WorkAxes.Item(2)
oSketch.OriginPoint = oPT

Dim oConstraint1 As TwoPointDistanceDimConstraint
Set oConstraint1 = oSketch.DimensionConstraints.Item(1)
oConstraint1.Parameter.Expression = "Depth"

Dim oConstraint2 As TwoPointDistanceDimConstraint
Set oConstraint2 = oSketch.DimensionConstraints.Item(2)
oConstraint2.Parameter.Expression = "Rebate"

Dim oSketch3D As Sketch3D
Set oSketch3D = oCompDef.Sketches3D.Add

Dim oLine As SketchLine3D
Set oLine = oSketch3D.Include(oEdge)

Dim oProfile As Profile
Set oProfile = oSketch.Profiles.AddForSolid

Dim oPath As Path
Set oPath = oCompDef.Features.CreatePath(oLine)

Dim spDef As SweepDefinition
Set spDef = oCompDef.Features.SweepFeatures.CreateSweepDefinition(kPathSweepType, oProfile, oPath, kCutOperation)

Dim sp As SweepFeature
Set sp = oCompDef.Features.SweepFeatures.Add(spDef)

End Sub

 

thanks for any help

 

 

Nacho
Automation & Design Engineer

Inventor automation Programmer (C#, VB.Net / iLogic)
Furniture, Sheet Metal, Structural, Metal fab, Tradeshow, Fabrication, CNC

EESignature


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.


0 Likes
1,387 Views
14 Replies
Replies (14)
Message 2 of 15

WCrihfield
Mentor
Mentor

Just at thought, but since your sketch isn't 'on' the selected face, and it's mainly for orientation, maybe selecting another Edge that is perpendicular to the other selected edge, would work better for orientation purposes.  Maybe you could extract a UnitVector (direction) from the first Edge, then use it to ensure the orientation (direction) of the sketch geometry.  Then the second edge is still setting the height of the sketch plane and ensures the sketch plane is perpendicular to the second selected edge, as it currently seems.

Also, just a tip, but when checking the edges against each other, you could simply check If e Is oEdge.

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 15

NachoShaw
Advisor
Advisor

Hi

 

Thanks for the reply. I originally did a If e = oEdge and did a test

 

dim e As Edge
For Each e in oFace.Edges
   If e = oEdge Then
      MsgBox("Found Edge")
   End If
Next

but i got a message flash for every edge so i didnt have much confidence it that approach..

 

Using a selected Edge would be ok, it wouldnt typically fit with the process though as the assumption would be to select a face to have the rebate cut into. maybe i could loop through the face edges to find one that works..

 

Thanks 🙂

 

Nacho
Automation & Design Engineer

Inventor automation Programmer (C#, VB.Net / iLogic)
Furniture, Sheet Metal, Structural, Metal fab, Tradeshow, Fabrication, CNC

EESignature


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.


0 Likes
Message 4 of 15

NachoShaw
Advisor
Advisor

or something else that could work- select the rebate face and the then the edge to find the intersecting edge. From a workflow standpoint, selecting faces would be better than edges as long as the cut profile can be correctly oriented

Nacho
Automation & Design Engineer

Inventor automation Programmer (C#, VB.Net / iLogic)
Furniture, Sheet Metal, Structural, Metal fab, Tradeshow, Fabrication, CNC

EESignature


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.


0 Likes
Message 5 of 15

NachoShaw
Advisor
Advisor

so i ran a bit of code to show me the UCS of each edge and i can see there is a difference which would help me set a direction. in this image, the left edge cut has the sketch aligned with the Y axis, the right sketch is aligned with the Z axis. If i could access the axis of the edge, i can change the sketch dimension constraints to align. For example:

 

get sketch longest edge

if it doesnt align with the Y axis, change the dimension constraints to switch values

 

 

Nacho
Automation & Design Engineer

Inventor automation Programmer (C#, VB.Net / iLogic)
Furniture, Sheet Metal, Structural, Metal fab, Tradeshow, Fabrication, CNC

EESignature


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.


0 Likes
Message 6 of 15

Stakin
Collaborator
Collaborator
without image
0 Likes
Message 7 of 15

NachoShaw
Advisor
Advisor

oops

 

Here is the image as referenced

ucs2.jpg

 

Furthermore, here is an image of the same edge between 2 faces with 2 UCS which leads me to think there are 2 edges, 1 for each face

2 ucs.jpg

 

In the meantime, i managed to figure out a way to determine the direction albeit i dont think its the method i am looking for. By looking at the Normal of each edge, there was a pattern difference between the ones that worked and the ones that didnt. I cased the pattern and changed the orientation of the dims which resolved my issue. Id like to think there is a better way to do this like compare the Y axis or check for a parallel but for now, this is working..

 

Cheers

 

Nacho
Automation & Design Engineer

Inventor automation Programmer (C#, VB.Net / iLogic)
Furniture, Sheet Metal, Structural, Metal fab, Tradeshow, Fabrication, CNC

EESignature


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.


0 Likes
Message 8 of 15

Stakin
Collaborator
Collaborator

Dim oSketch As PlanarSketch Set oSketch = oCompDef.Sketches.AddWithOrientation(oWP, oEdge, True, True, oEdge.StartVertex, True)

 

the SketchOrientation is not a 3D Direction,

Stakin_0-1616378550505.png

oEdge is perpendicular to the sketch plane,it cann't be sketch X、Y direction。

 

For this instance,you can use the vector which is cross product of the selceted edge's  Normal and the Selected face' normal.

And the origin,you should use the midpoint of the oedge

 

 

 

0 Likes
Message 9 of 15

Stakin
Collaborator
Collaborator

on the sketchplane ,it‘s UCS just two direction,and just assign one Xdirection for it,the other Ydirection will determine with Xdirection and the sketchplane’s Nomal by Right hand rule。

  

0 Likes
Message 10 of 15

NachoShaw
Advisor
Advisor

thanks for that. The -

Set oSketch = oCompDef.Sketches.AddWithOrientation(oWP, oEdge, True, True, oEdge.StartVertex, True)

was me testing if thats what i needed but i suspected it wasnt. I have reverted back to 

Set oSketch = oCompDef.Sketches.Add(oWP, False)

 

 

Nacho
Automation & Design Engineer

Inventor automation Programmer (C#, VB.Net / iLogic)
Furniture, Sheet Metal, Structural, Metal fab, Tradeshow, Fabrication, CNC

EESignature


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.


0 Likes
Message 11 of 15

WCrihfield
Mentor
Mentor

Hi @NachoShaw.  Sorry for the delay...I've been away a few days.  And now that I'm back, things at work have gotten busy.

In my earlier post about checking the edge variables, I didn't say to check it like "If e = oEdge", I said it would likely be easier to check it like "If e Is oEdge", using the operator "Is", instead of equals (=).  When comparing some objects (not information data variables), it is sometimes easiest/best to use "Is" when comparing, rather than =, that's all.  Sorry for the confusion.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 12 of 15

NachoShaw
Advisor
Advisor

Hey

 

No worries mate. Makes sense to Is instead of =. Im thinking of a different approach now as the current method seems a little problematic 🙂

 

Im going to look at using SurfaceGraphics to give me a feature preview and process the feature after the selection is complete. The method i was using added the feature per edge and previewed it with a Transaction but that was becoming too complicated for what it was. 🙂

 

 

Nacho
Automation & Design Engineer

Inventor automation Programmer (C#, VB.Net / iLogic)
Furniture, Sheet Metal, Structural, Metal fab, Tradeshow, Fabrication, CNC

EESignature


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.


0 Likes
Message 13 of 15

Stakin
Collaborator
Collaborator

 

PlanarSketches.AddWithOrientation( PlanarEntity As Object, AxisEntity As Object, NaturalAxisDirection As Boolean, AxisIsX As Boolean, Origin As Object, [UseFaceEdges] As Boolean )

 

you can try it agian, AxisEntity Use the below UnitVector -----  oXvector(it aline with the face direction)

                                    Origin        Use the oEdge.MidPoint

 

 

dim oXvector As UnitVector
Dim oLineSeg As LineSegment
oLineSeg = oEdge.Geometry


Dim eval As SurfaceEvaluator
    eval = oFace.Evaluator
    
    Dim center(1) As Double
    center(0) = (eval.ParamRangeRect.MinPoint.X + eval.ParamRangeRect.MaxPoint.X) / 2
    center(1) = (eval.ParamRangeRect.MinPoint.y + eval.ParamRangeRect.MaxPoint.y) / 2
    Dim normal() As Double
    Call eval.GetNormal(center, normal)
    GetFaceNormal = ThisApplication.TransientGeometry.CreateUnitVector(normal(0), normal(1), normal(2))
oXvector= oLineSeg.Direction.CrossProduct( GetFaceNormal)

 

 

0 Likes
Message 14 of 15

NachoShaw
Advisor
Advisor

much appreciated for you reply. I tried the code but couldnt get it work It kept giving me an error on the CreateSketchWithOrientation - Invalid procedure call or argument

 

Set oSketch = oCompDef.Sketches.AddWithOrientation(oWP, oXvector, True, True, oLineSeg.MidPoint)

 

oEdge doesnt have MidPoint

 

 

Thanks

 

Nacho
Automation & Design Engineer

Inventor automation Programmer (C#, VB.Net / iLogic)
Furniture, Sheet Metal, Structural, Metal fab, Tradeshow, Fabrication, CNC

EESignature


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.


0 Likes
Message 15 of 15

Stakin
Collaborator
Collaborator
Dim oWkAxes As WorkAxes 
Set oWkAxes=oCompDef.WorkAxes.AddFixed(oLineSeg.MidPoint,oXvector,1) 
Set oSketch = oCompDef.Sketches.AddWithOrientation(oWP, oWkAxes, True, True, oLineSeg.MidPoint)
0 Likes