Rotate the sketch and add constraint

Rotate the sketch and add constraint

murugasubi
Enthusiast Enthusiast
1,148 Views
5 Replies
Message 1 of 6

Rotate the sketch and add constraint

murugasubi
Enthusiast
Enthusiast

Dear, 

Please refer to the attached sample image. I am copying the sketch from AutoCAD to the inventor. Sometimes it's coming in another direction. Hence, I want to rotate the sketch. 

Secondly, I will pick any point that has to be constrained coincidently with the origin. 

 

Please help me with ilogic code.  This will save a lot of time for us.

 

I have tried with the below code for rotating the sketch, but not working. 

'Rotate sketch
Sub
main Dim oPartDoc As PartDocument = ThisApplication.ActiveDocument Dim oDef As PartComponentDefinition = oPartDoc.ComponentDefinition Dim oPoint As Point = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAllPointEntities, "Select a Point.") Dim oMoveFeatDef As MoveDefinition = oDef.Features.MoveFeatures.CreateMoveDefinition(oPoint) Dim oAxis As WorkAxis = oDef.WorkAxes.Item(1) oMoveFeatDef.AddRotateAboutAxis(oAxis, False, "90 deg") End Sub

'For adding Coincident Constraint with origin, This is also not working.
Sub
main Dim oPartDoc As PartDocument = ThisDoc.Document Dim oFeatures As PartFeatures = oPartDoc.ComponentDefinition.Features Dim oCompDef As PartComponentDefinition oCompDef = oPartDoc.ComponentDefinition Dim pointA = ThisDoc.Geometry.Point(0, 0, 0) Dim PointB As Point = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAllPointEntities, "Select a Point.") Dim oSketches1 As PlanarSketches = oPartDoc.ComponentDefinition.Sketches oSketches1("WHBaseSketch").GeometricConstraints.AddCoincident(pointA, PointB) End Sub
 

 

 

0 Likes
Accepted solutions (3)
1,149 Views
5 Replies
Replies (5)
Message 2 of 6

WCrihfield
Mentor
Mentor
Accepted solution

Hi @murugasubi.  This one is a bit complicated to explain.

The Move feature is for moving surface bodies (and solid bodies), not for moving sketches. To rotate the sketch, you need to first find/get the sketch itself (PlanarSketch).  You will then need actually move all the sketch entities in the sketch (at once) from the selected point on your existing geometry to the desired origin point, which you can define within the code.  Then you will need to rotate all the sketch entities at once so that the selected linear sketch line you select will be aligned with the natural X axis direction of the 2D sketch environment.  Those two actions should result in accomplishing what I believe you are trying to do.  If you just attempt to redefine the origin point and axis entity of the sketch, it won't move or rotate the geometry like you are wanting.


Here is an example of an iLogic rule that should help you reestablish both aspects of the sketch the way you want it.

Dim oPDoc As PartDocument = ThisDoc.Document
Dim oPDef As PartComponentDefinition = oPDoc.ComponentDefinition
oCM = ThisApplication.CommandManager
Dim oTO As TransientObjects = ThisApplication.TransientObjects
Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
Dim oSketch As PlanarSketch = oPDef.Sketches.Item(1)
Dim oAllEntities As ObjectCollection = oTO.CreateObjectCollection(oSketch.SketchEntities)

oObj = oCM.Pick(SelectionFilterEnum.kSketchPointFilter, "Select point on sketch geometry to align with origin.")
If oObj Is Nothing Then Exit Sub
If Not TypeOf oObj Is SketchPoint Then Exit Sub
Dim oSourcePoint As SketchPoint = oObj

'identify point to move that point to
Dim oTargetOriginPoint As Point2d = oTG.CreatePoint2d(0,0)

'calculate Vector to use for the Move method
oVector = oSourcePoint.Geometry.VectorTo(oTargetOriginPoint)
'move all sketch entities, as group, to new location
oSketch.MoveSketchObjects(oAllEntities, oVector, False, False)

'select a linear sketch line you want to align with X axis of sketch after sketch is rotated
oObj2  = oCM.Pick(SelectionFilterEnum.kSketchCurveLinearFilter, "Select linear sketch line to align with X Axis of sketch.")
If oObj2 Is Nothing Then Exit Sub
If Not TypeOf oObj2 Is SketchLine Then Exit Sub
Dim oSLine As SketchLine = oObj2

'create direction to compare to (straight right in 2D space)
Dim oXDir As UnitVector2d = oTG.CreateUnitVector2d(1, 0)

'measure angle between that line and natural X axis line/direction of sketch environment
oAngle = oXDir.AngleTo(oSLine.Geometry.Direction)
'control angle, so it won't flip over too far in wrong direction
If oAngle < Math.PI Then oAngle = -oAngle
	
'rotate all sketch entities, as group, to align selected line with x-axis of sketch
oSketch.RotateSketchObjects(oAllEntities, oTargetOriginPoint, oAngle, False, False)

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

If you want and have time, I would appreciate your Vote(s) for My IDEAS 💡 or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 6

fidel.makatiaD5W7V
Alumni
Alumni
Accepted solution

Hi @murugasubi , you can also go through this code from inventor 2022 help documentation for editing a sketch orientation 

Inventor 2022 Help | Sketch Edit Orientation | Autodesk

Let me know if it helps or in case of any clarifications 

 



Fidel Makatia
Developer Advocate

href=https://help.autodesk.com/view/INVNTOR/2022/ENU/?guid=GUID-0BD48573-7193-4285-87B7-6727555D053E rel= "noopener noreferrer">Inventor 2022 Documentation |
Message 4 of 6

murugasubi
Enthusiast
Enthusiast

Hi @WCrihfield ,  First of all, thank you very much for your help. 

 

I have tried with your code, but I am getting errors on the moveSketchObjects and RotateSketchObjects lines. "Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))".

 

I have attached the part and error msg. Please help me resolve this issue.

 

Thanks in advance.

0 Likes
Message 5 of 6

WCrihfield
Mentor
Mentor
Accepted solution

Hi @murugasubi.  I know it will throw an error if the sketch contains any projected or fixed geometry, because I ran into that issue while testing on my own file.  The code I posted will likely not be perfect for every similar situation, but it was working OK in a simple test file I was working with at the time.  I see that the geometry in your part file sketch is actually a SketchBlock, instead of just a bunch of manually placed sketch geometry.  That apparently makes a difference, because my code doesn't appear to be working for that situation.  So, what I had to do to move it was actually select just that SketchBlock, then change the value of its Position property, to the (0,0) position to move it to that point.  That worked like a charm.  The SketchBlock object also has a Property called Transformation which represents its Matrix2d object (3x3 grid of numbers that defines not only its position, but also its orientation/angle).  To rotate the block, I had to manipulate that a bit, using one of its built-in methods called SetToRotation.  Naturally, these types of things use metric values by default and radians, instead of degrees for angles, so I had to do a bit of unit conversion to rotate the block 90 degrees clockwise, as your original image indicates.

Here is the resulting code that worked on the model file you provided.

Dim oPDoc As PartDocument = ThisDoc.Document
Dim oPDef As PartComponentDefinition = oPDoc.ComponentDefinition
Dim oSketch As PlanarSketch = oPDef.Sketches.Item(1)

oBlk = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kSketchBlockFilter, "Select a Sketch Block to move.")
If oBlk Is Nothing Then Exit Sub
If Not TypeOf oBlk Is SketchBlock Then Exit Sub
Dim oSkBlock As SketchBlock = oBlk
oOriginPoint2d = ThisApplication.TransientGeometry.CreatePoint2d(0, 0)
oSkBlock.Position = oOriginPoint2d
oMatrix2d = oSkBlock.Transformation
'-(System.Math.PI / 2) = the Radians equivalent of -90 degrees or 90 degrees in clockwise direction
oMatrix2d.SetToRotation(-(System.Math.PI / 2), oOriginPoint2d)
oSkBlock.Transformation = oMatrix2d

oPDoc.Update

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 6 of 6

murugasubi
Enthusiast
Enthusiast

Thanks a lot, @WCrihfield.

I have checked with many sketches, all are working excellent. I really appreciate your efforts and heartfelt thanks to you. You have saved a lot of time for us on sketch editing.

 

0 Likes