[Macro] Create rectangle and center it to main projected planes with symetric constrain

[Macro] Create rectangle and center it to main projected planes with symetric constrain

amarcoc
Advocate Advocate
964 Views
9 Replies
Message 1 of 10

[Macro] Create rectangle and center it to main projected planes with symetric constrain

amarcoc
Advocate
Advocate

Hi.

 

I am trying to make a macro (iLogic or VBA) that generates a rectagle, simetrical to main planes.

Here are the steps:
1) Create new sketch [DONE]
2) Select Plane [DONE]
3) Project all main planes [DONE]
4) Create a rectangle [DONE]
5) Center rectangle with symetric constrain

Can anyone help me on the missing points? Assuming I am already inside a sketch, I only need the create rectangle and center it.

Thanks in advance!!!

 

EDIT: Here is the code used to create the rectangle. How do I center it to main planes? It's the only thing missing. Thanks!

 

Dim oCompDef As PartComponentDefinition
oCompDef = ThisApplication.ActiveDocument.ComponentDefinition

Dim planeXY = oCompDef.WorkPlanes.Item(3)
Dim my_sketch As PlanarSketch
my_sketch = oCompDef.Sketches.Add(planeXY)
my_sketch.OriginPoint = oCompDef.WorkPoints.Item(1)
my_sketch.Edit

'PROJECT PLANES
Dim oPartDoc As PartDocument
Dim oAssyDoc As AssemblyDocument
Dim oSelectSet As SelectSet
Select Case ThisApplication.ActiveDocument.DocumentType
	Case kPartDocumentObject
		oPartDoc = ThisApplication.ActiveDocument
		oSelectSet = oPartDoc.SelectSet
		oSelectSet.Clear 
		For Each oWorkPlane In oPartDoc.ComponentDefinition.WorkPlanes
			oSelectSet.Select(oWorkPlane)
		Next
	Case kAssemblyDocumentObject
		oAssyDoc = ThisApplication.ActiveDocument
		oSelectSet = oAssyDoc.SelectSet
		For Each oWorkPlane In oAssyDoc.ComponentDefinition.WorkPlanes
			oSelectSet.Select(oWorkPlane)
		Next
End Select
ThisApplication.CommandManager.ControlDefinitions.Item("AppProjectGeometryWrapperCmd").Execute
ThisApplication.CommandManager.StopActiveCommand


Dim Width As Integer = 40
Dim Height As Integer = 80

Dim tg As Inventor.TransientGeometry = ThisApplication.TransientGeometry
Dim lines As Inventor.SketchLines = my_sketch.SketchLines
Dim points As Inventor.SketchPoints = my_sketch.SketchPoints

Dim point_array(3) As Inventor.SketchPoint
point_array(0) = points.Add(tg.CreatePoint2d(-Width/2, -Height/2), False)
point_array(1) = points.Add(tg.CreatePoint2d(Width/2, -Height/2), False)
point_array(2) = points.Add(tg.CreatePoint2d(Width/2, Height/2), False)
point_array(3) = points.Add(tg.CreatePoint2d(-Width/2, Height/2), False)

'Draw lines
Dim line0 = lines.AddByTwoPoints(point_array(0), point_array(1))
Dim line1 = lines.AddByTwoPoints(point_array(1), point_array(2))
Dim line2 = lines.AddByTwoPoints(point_array(2), point_array(3))
Dim line3 = lines.AddByTwoPoints(point_array(3), point_array(0))

'constrain geometry
Dim constr As Inventor.GeometricConstraints
constr = my_sketch.GeometricConstraints

'horizontal constraints
constr.AddHorizontal(line0)
constr.AddHorizontal(line2)
'vertical constraints
constr.AddVertical(line1)
constr.AddVertical(line3)
'equal constraints
constr.AddEqualLength(line0, line1)
'Add Dimensions
my_sketch.DimensionConstraints.AddOffset(line0, line2.StartSketchPoint, tg.CreatePoint2d(5, 2), False, False)


ThisApplication.CommandManager.ControlDefinitions.Item("SketchHideAllConstraintsCtxCmd").Execute

 

 

0 Likes
Accepted solutions (1)
965 Views
9 Replies
Replies (9)
Message 2 of 10

JhoelForshav
Mentor
Mentor

Hi @amarcoc 

I'm guessing the sketch is on one of the coordinate system planes and you want to constrain the rectangle to the other two?

 

Try this 🙂

 

If Not TypeOf ThisApplication.ActiveEditObject Is PlanarSketch Then
	MessageBox.Show("A sketch must be active.", "iLogic")
	Return
End If

Dim oSketch As PlanarSketch = ThisApplication.ActiveEditObject
Dim oTransGeom As TransientGeometry = ThisApplication.TransientGeometry

Dim Width As Integer = 40
Dim Height As Integer = 40

' Create a rectangle 
Dim oRectangleLines As SketchEntitiesEnumerator
oRectangleLines = oSketch.SketchLines.AddAsTwoPointRectangle( _
oTransGeom.CreatePoint2d(-Width / 2, -Height / 2), _
oTransGeom.CreatePoint2d(Width / 2, Height / 2))

For i = 1 To 3
	Dim oPlane As WorkPlane = ThisDoc.Document.ComponentDefinition.WorkPlanes(i)
	If oPlane.IsCoordinateSystemElement AndAlso oPlane IsNot oSketch.PlanarEntity
		'Project the plane
		Dim wpLine As SketchLine = oSketch.AddByProjectingEntity(oPlane)
		wpLine.Construction = True
		'Get the two lines that are parallell to the projected plane
		Dim oLines As IEnumerable(Of SketchLine) = oSketch.SketchLines.OfType(Of SketchLine).Where(Function(t As SketchLine) _
		t IsNot wpLine AndAlso t.Geometry.Direction.IsParallelTo(wpLine.Geometry.Direction))
		'Create symmetric constraint
		oSketch.GeometricConstraints.AddSymmetry(oLines(0), oLines(1), wpLine)
	End If
Next

iLogicVb.UpdateWhenDone = True
0 Likes
Message 3 of 10

amarcoc
Advocate
Advocate

Wow!!!

 

That is perfect. One final request:  How can I add X and Y dimension between lines (not points)?

 

EDIT: And also, how can I hide all constrains after creation?

0 Likes
Message 4 of 10

JhoelForshav
Mentor
Mentor

@amarcoc 

To set dimension between sketchlines, use DimensionConstraints.AddOffset:

 

If Not TypeOf ThisApplication.ActiveEditObject Is PlanarSketch Then
	MessageBox.Show("A sketch must be active.", "iLogic")
	Return
End If

Dim oSketch As PlanarSketch = ThisApplication.ActiveEditObject
Dim oTransGeom As TransientGeometry = ThisApplication.TransientGeometry

Dim Width As Integer = 40
Dim Height As Integer = 40

' Create a rectangle 
Dim oRectangleLines As SketchEntitiesEnumerator
oRectangleLines = oSketch.SketchLines.AddAsTwoPointRectangle( _
oTransGeom.CreatePoint2d(-Width / 2, -Height / 2), _
oTransGeom.CreatePoint2d(Width / 2, Height / 2))

For i = 1 To 3
	Dim oPlane As WorkPlane = ThisDoc.Document.ComponentDefinition.WorkPlanes(i)
	If oPlane.IsCoordinateSystemElement AndAlso oPlane IsNot oSketch.PlanarEntity
		'Project the plane
		Dim wpLine As SketchLine = oSketch.AddByProjectingEntity(oPlane)
		wpLine.Construction = True
		'Get the two lines that are parallell to the projected plane
		Dim oLines As IEnumerable(Of SketchLine) = oSketch.SketchLines.OfType(Of SketchLine).Where(Function(t As SketchLine) _
		t IsNot wpLine AndAlso t.Geometry.Direction.IsParallelTo(wpLine.Geometry.Direction))
		'Create symmetric constraint
		oSketch.GeometricConstraints.AddSymmetry(oLines(0), oLines(1), wpLine)
		Dim oTextPoint As Point2d
		oTextPoint = oSketch.ModelToSketchSpace(ThisDoc.Document.ComponentDefinition.WorkPoints(1).Point)
		Dim oVector As Vector2d = oLines(0).Geometry.Direction.AsVector
		oVector.ScaleBy(oLines(0).Length / 2 * 1.05)
		oTextPoint.TranslateBy(oVector)
		oSketch.DimensionConstraints.AddOffset(oLines(0), oLines(1), oTextPoint, False)
	End If
Next

iLogicVb.UpdateWhenDone = True
0 Likes
Message 5 of 10

amarcoc
Advocate
Advocate

Thank you!

 

Is it possible to auto-hide all the generated constrains? (same as F9 shortcut)

0 Likes
Message 6 of 10

amarcoc
Advocate
Advocate

Also, can I input dimension value when it is created?

0 Likes
Message 7 of 10

JhoelForshav
Mentor
Mentor

I couldn't find a way to hide constraints other than through the commandmanager. I hope thats ok.

To change the dimension value, you actually change the model parameter tied to the dimension constraint.

See example below:

 

If Not TypeOf ThisApplication.ActiveEditObject Is PlanarSketch Then
	MessageBox.Show("A sketch must be active.", "iLogic")
	Return
End If

Dim oSketch As PlanarSketch = ThisApplication.ActiveEditObject
Dim oTransGeom As TransientGeometry = ThisApplication.TransientGeometry

Dim Width As Integer = 40
Dim Height As Integer = 40

' Create a rectangle 
Dim oRectangleLines As SketchEntitiesEnumerator
oRectangleLines = oSketch.SketchLines.AddAsTwoPointRectangle( _
oTransGeom.CreatePoint2d(-Width / 2, -Height / 2), _
oTransGeom.CreatePoint2d(Width / 2, Height / 2))

For i = 1 To 3
	Dim oPlane As WorkPlane = ThisDoc.Document.ComponentDefinition.WorkPlanes(i)
	If oPlane.IsCoordinateSystemElement AndAlso oPlane IsNot oSketch.PlanarEntity
		'Project the plane
		Dim wpLine As SketchLine = oSketch.AddByProjectingEntity(oPlane)
		wpLine.Construction = True
		'Get the two lines that are parallell to the projected plane
		Dim oLines As IEnumerable(Of SketchLine) = oSketch.SketchLines.OfType(Of SketchLine).Where(Function(t As SketchLine) _
		t IsNot wpLine AndAlso t.Geometry.Direction.IsParallelTo(wpLine.Geometry.Direction))
		'Create symmetric constraint
		oSketch.GeometricConstraints.AddSymmetry(oLines(0), oLines(1), wpLine)
		Dim oTextPoint As Point2d
		oTextPoint = oSketch.ModelToSketchSpace(ThisDoc.Document.ComponentDefinition.WorkPoints(1).Point)
		Dim oVector As Vector2d = oLines(0).Geometry.Direction.AsVector
		oVector.ScaleBy(oLines(0).Length / 2 * 1.05)
		oTextPoint.TranslateBy(oVector)
		Dim oOffset As OffsetDimConstraint = oSketch.DimensionConstraints.AddOffset(oLines(0), oLines(1), oTextPoint, False)
		'Set parameter value
		oOffset.Parameter.Expression = "30 mm"
	End If
Next
'Let the application process queue 
ThisApplication.UserInterfaceManager.DoEvents()
'Hide all constraints
ThisApplication.CommandManager.ControlDefinitions.Item("SketchHideAllConstraintsCtxCmd").Execute
iLogicVb.UpdateWhenDone = True
0 Likes
Message 8 of 10

amarcoc
Advocate
Advocate

Thank you. 😃

 

That way, I define a square. Any way to define Width different than Height?

0 Likes
Message 9 of 10

JhoelForshav
Mentor
Mentor
Accepted solution

@amarcoc 

See example below:

If Not TypeOf ThisApplication.ActiveEditObject Is PlanarSketch Then
	MessageBox.Show("A sketch must be active.", "iLogic")
	Return
End If

Dim oSketch As PlanarSketch = ThisApplication.ActiveEditObject
Dim oTransGeom As TransientGeometry = ThisApplication.TransientGeometry

Dim Width As Integer = 40
Dim Height As Integer = 40

' Create a rectangle 
Dim oRectangleLines As SketchEntitiesEnumerator
oRectangleLines = oSketch.SketchLines.AddAsTwoPointRectangle( _
oTransGeom.CreatePoint2d(-Width / 2, -Height / 2), _
oTransGeom.CreatePoint2d(Width / 2, Height / 2))

Dim oXParam As Inventor.Parameter
Dim oYParam As Inventor.Parameter

For i = 1 To 3
	Dim oPlane As WorkPlane = ThisDoc.Document.ComponentDefinition.WorkPlanes(i)
	If oPlane.IsCoordinateSystemElement AndAlso oPlane IsNot oSketch.PlanarEntity
		'Project the plane
		Dim wpLine As SketchLine = oSketch.AddByProjectingEntity(oPlane)
		wpLine.Construction = True
		'Get the two lines that are parallell to the projected plane
		Dim oLines As IEnumerable(Of SketchLine) = oSketch.SketchLines.OfType(Of SketchLine).Where(Function(t As SketchLine) _
		t IsNot wpLine AndAlso t.Geometry.Direction.IsParallelTo(wpLine.Geometry.Direction))
		'Create symmetric constraint
		oSketch.GeometricConstraints.AddSymmetry(oLines(0), oLines(1), wpLine)
		Dim oTextPoint As Point2d
		oTextPoint = oSketch.ModelToSketchSpace(ThisDoc.Document.ComponentDefinition.WorkPoints(1).Point)
		Dim oVector As Vector2d = oLines(0).Geometry.Direction.AsVector
		oVector.ScaleBy(oLines(0).Length / 2 * 1.05)
		oTextPoint.TranslateBy(oVector)
		Dim oOffset As OffsetDimConstraint = oSketch.DimensionConstraints.AddOffset(oLines(0), oLines(1), oTextPoint, False)
		
		
		If DoubleForEquals.IsEqual(oVector.X, 0) Then oXParam = oOffset.Parameter
		If DoubleForEquals.IsEqual(oVector.Y, 0) Then oYParam = oOffset.Parameter

	End If
Next

oXParam.Expression = "100 mm"
oYParam.Expression = "50 mm"

'Let the application process que 
ThisApplication.UserInterfaceManager.DoEvents()
'Hide all constraints
ThisApplication.CommandManager.ControlDefinitions.Item("SketchHideAllConstraintsCtxCmd").Execute
iLogicVb.UpdateWhenDone = True
Message 10 of 10

amarcoc
Advocate
Advocate

Thank you. That is perfect now 🙂

0 Likes