iLogic Use line from sketch for rectangular Pattern Direction

iLogic Use line from sketch for rectangular Pattern Direction

neodd70
Enthusiast Enthusiast
1,439 Views
4 Replies
Message 1 of 5

iLogic Use line from sketch for rectangular Pattern Direction

neodd70
Enthusiast
Enthusiast

I have iLogic code to create a new sketch, draw a line on that sketch and then create a hole and a rectangular pattern of that hole. I am trying to use the line I created in the sketch as the direction line for the pattern so i can set the pattern to curve length. This way my hole pattern will auto update if my part changes size. My problem is that i cannot seem to figure out how to have the pattern reference the line in my sketch as the direction curve. Here is what I have so far. Any help would be greatly appreciated.

Class ThisRule
	Dim oPartDoc As Document
	Dim oPartCompDef As PartComponentDefinition
	Dim oTG As TransientGeometry
	Dim oSketchPoint As SketchPoint
	Dim SketchPointParentSketch As PlanarSketch
	Dim vectorForCheck As UnitVector
	Dim pointToCheck As Point
	Dim foundObjects As ObjectsEnumerator
	Dim locationPoints As ObjectsEnumerator	
	Dim oFace As Face
	Dim oFrontEdge As Edge
	Dim oLeftEdge As Edge
	Dim oRightEdge As Edge
	Dim oSketch As PlanarSketch
	Dim oQty As Integer
	Dim oFromLeftEdge As Double
	Dim oFromRightEdge As Double
	Dim oFromFrontEdge As Double
	Dim oFrontEdgeDistance As Double
	Dim oPatternSpacing As Double
End Class

Sub Main()
	oPartDoc = ThisDoc.Document
	oPartCompDef = ThisApplication.ActiveDocument.ComponentDefinition
	oTG = ThisApplication.TransientGeometry

'get measurements to determine Material Thickness
Dim My_x As Double = Measure.ExtentsLength
Dim My_y As Double = Measure.ExtentsWidth
Dim My_z As Double = Measure.ExtentsHeight

'Make shortest value the Thickness, rounded to 4 places
Dim oThickness As Double = Round(MinOfMany(My_x, My_y, My_z), 4)

'Select Face and Edges to dimension sketch circles from
oFace = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFaceFilter, "Select Surface to Place Holes")
oFrontEdge = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartEdgeFilter, "Select Front Edge of Pattern")
oLeftEdge = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartEdgeFilter, "Select Left Edge of Pattern")
oRightEdge = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartEdgeFilter, "Select Right Edge of Pattern")

Dim oMaterial As String = MaterialType
Dim oHoleType As String = HoleType
Dim oHoleSize As Double = HoleSizes * 2.54

'Add Sketch and set sketch origin point to the startVertex of the FrontEdge

oSketch = oPartCompDef.Sketches.Add(oFace, False)
oSketch.AxisEntity = oFrontEdge
oSketch.OriginPoint = oFrontEdge.StartVertex
oSketch.NaturalAxisDirection = True

'Create the name for the sketch based on user assigned parameters
Dim oSketchNameSuffix As Integer
Dim oNameCompare As String
Dim oNameNumCount As Integer
Dim oNameCharCount As Integer

If oHoleType = "Thru" Then
	oSketchNameSuffix = 0
	oNameCompare = "SK_Hole_"
	oNameCharCount = 8
	oNameNumCount = 9

Else If oHoleType = "Pilot" Then
	oSketchNameSuffix = 0
	oNameCompare = "SK_Pilot_"
	oNameCharCount = 9
	oNameNumCount = 10

End If

'Check all sketches in the model tree and compare the names to the
'name of the newly created sketch and if name exists increment the
'suffix by 1. Continue until the newly created sketch name is unique
'and can be used.
	For Each oSketchNameCheck As Sketch In oPartCompDef.Sketches
		If Left(oSketchNameCheck.Name,oNameCharCount) = oNameCompare Then
			Dim oSketchNumber As Integer = Val(Mid(oSketchNameCheck.Name, oNameNumCount, 2))			
	        While oSketchNameSuffix <= oSketchNumber
				oSketchNameSuffix = oSketchNameSuffix + 1
			End While
		End If	
	Next 
	
oSketch.Name = oNameCompare & oSketchNameSuffix

'QTY of holes and distance from front, left and right edges
oQty = 4
oFrontEdgeDistance = ThisApplication.MeasureTools.GetMinimumDistance(oFrontEdge.StartVertex, oFrontEdge.StopVertex)
oFromLeftEdge = FromLeftEdge * 2.54
oFromRightEdge = oFrontEdgeDistance - (FromRightEdge *2.54)
oFromFrontEdge = FromFrontEdge * 2.54
oPatternSpacing = (oFrontEdgeDistance - (oFromLeftEdge + oFromRightEdge)) / (oQty - 1)

Call oSketch.SketchLines.AddByTwoPoints(oTG.CreatePoint2d(oFromLeftEdge, oFromFrontEdge), oTG.CreatePoint2d(oFromRightEdge, oFromFrontEdge))
'Dim oSketchLine As SketchEntitiesEnumerator = oSketch.SketchLines.Item(1)
'Dim oLine as oSketchLines = oSketch.SketchLines.Item(1)

'create a sketch point to check if the coordinate system needs
'To be flipped so the circles fall inside the parent part.

OriginCheck()
	If (foundObjects.Count = 0) Then
		oSketch.NaturalAxisDirection = False
		OriginCheck()
		If (foundObjects.Count = 0) Then
			oSketch.NaturalAxisDirection = True
			oSketch.OriginPoint = oFrontEdge.StopVertex
			OriginCheck()
			If (foundObjects.Count = 0) Then
				oSketch.NaturalAxisDirection = False
				OriginCheck()
			End If
		End If
	End If
	
oSketchPoint.Delete



Dim oPoint As Point = oSketch.SketchToModelSpace(oTG.CreatePoint2d(oFromleftEdge, oFromFrontEdge))
Dim oLinearPlacementDef As LinearHolePlacementDefinition = oPartCompDef.Features.HoleFeatures.CreateLinearPlacementDefinition(oFace, oLeftEdge, oFromLeftEdge, oFrontEdge, oFromFrontEdge, oPoint)
Call oPartCompDef.Features.HoleFeatures.AddDrilledByThroughAllExtent(oLinearPlacementDef, ".156in", kPositiveExtentDirection)

' Create the object collection.
Dim FeatureColl As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection

' Add the newly created hole feature to the collection.
Call FeatureColl.Add(oPartCompDef.Features.HoleFeatures.Item(1))

' Number of elements (X axis)
Dim NbrOfHolesLength As Integer
NbrOfHolesLength = 2

' Distance between elements.
Dim DistanceHolesLength As Double
DistanceHolesLength = 1

' Number of elements (Y axis)
Dim NbrOfHolesWidth As Integer
NbrOfHolesWidth = 1

' Distance between elements.
Dim DistanceHolesWidth As Double
DistanceHolesWidth = 1

' Create the pattern.
'Call oPartCompDef.Features.RectangularPatternFeatures.Add(FeatureColl, oFrontEdge, True, NbrOfHolesLength, _
'DistanceHolesLength * 2.54, kDefault, , oLeftEdge, True, NbrOfHolesWidth, DistanceHolesWidth * 2.54, _
'kDefault, , , kIdentical)

Call oPartCompDef.Features.RectangularPatternFeatures.Add(FeatureColl, oFrontEdge, True, NbrOfHolesLength, _
DistanceHolesLength * 2.54, PatternSpacingTypeEnum.kFitToPathLength, , , , , , , , , kIdentical)

'oSketch.Delete()

End Sub

Public Sub OriginCheck()
	oSketchPoint = oSketch.SketchPoints.Add(oTG.CreatePoint2d(oFromLeftEdge, oFromFrontEdge))
	SketchPointParentSketch = oSketchPoint.Parent
	vectorForCheck = SketchPointParentSketch.PlanarEntityGeometry.Normal
	pointToCheck = oSketchPoint.Geometry3d
	foundObjects = Nothing
	locationPoints = Nothing
	oPartCompDef.FindUsingRay(pointToCheck, vectorForCheck, .00001, foundObjects, locationPoints)
End Sub
0 Likes
Accepted solutions (1)
1,440 Views
4 Replies
Replies (4)
Message 2 of 5

Sergio.D.Suárez
Mentor
Mentor

Hello, when you work with a code that refers to so many objects with personalized names, it is difficult to test the code. However, I have placed the instruction on error resume next, and created the pattern, the error I suppose is not found in how you have defined the pattern, if in the loading of data such as distance, instances, etc.
also corregi in the beginning, the definitions of global variables.
I hope you can solve your problem. regards

 

Class ThisRule
	Dim oPartDoc As Document
	Dim oPartCompDef As PartComponentDefinition
	Dim oTG As TransientGeometry
	Dim oSketchPoint As SketchPoint
	Dim SketchPointParentSketch As PlanarSketch
	Dim vectorForCheck As UnitVector
	Dim pointToCheck As Point
	Dim foundObjects As ObjectsEnumerator
	Dim locationPoints As ObjectsEnumerator	
	Dim oFace As Face
	Dim oFrontEdge As Edge
	Dim oLeftEdge As Edge
	Dim oRightEdge As Edge
	Dim oSketch As PlanarSketch
	Dim oQty As Integer
	Dim oFromLeftEdge As Double
	Dim oFromRightEdge As Double
	Dim oFromFrontEdge As Double
	Dim oFrontEdgeDistance As Double
	Dim oPatternSpacing As Double

Sub Main()
	oPartDoc = ThisDoc.Document
	oPartCompDef = ThisApplication.ActiveDocument.ComponentDefinition
	oTG = ThisApplication.TransientGeometry

'get measurements to determine Material Thickness
Dim My_x As Double = Measure.ExtentsLength
Dim My_y As Double = Measure.ExtentsWidth
Dim My_z As Double = Measure.ExtentsHeight

'Make shortest value the Thickness, rounded to 4 places
Dim oThickness As Double = Round(MinOfMany(My_x, My_y, My_z), 4)

'Select Face and Edges to dimension sketch circles from
oFace = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFaceFilter, "Select Surface to Place Holes")
If oFace Is Nothing Then Exit Sub
oFrontEdge = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartEdgeFilter, "Select Front Edge of Pattern")
If oFrontEdge Is Nothing Then Exit Sub
oLeftEdge = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartEdgeFilter, "Select Left Edge of Pattern")
If oLeftEdge Is Nothing Then Exit Sub
oRightEdge = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartEdgeFilter, "Select Right Edge of Pattern")
If oRightEdge Is Nothing Then Exit Sub

Dim oMaterial As String = MaterialType
Dim oHoleType As String = HoleType
Dim oHoleSize As Double = HoleSizes * 2.54

'Add Sketch and set sketch origin point to the startVertex of the FrontEdge

oSketch = oPartCompDef.Sketches.Add(oFace, False)
oSketch.AxisEntity = oFrontEdge
oSketch.OriginPoint = oFrontEdge.StartVertex
oSketch.NaturalAxisDirection = True

'Create the name for the sketch based on user assigned parameters
Dim oSketchNameSuffix As Integer
Dim oNameCompare As String
Dim oNameNumCount As Integer
Dim oNameCharCount As Integer

If oHoleType = "Thru" Then
	oSketchNameSuffix = 0
	oNameCompare = "SK_Hole_"
	oNameCharCount = 8
	oNameNumCount = 9

Else If oHoleType = "Pilot" Then
	oSketchNameSuffix = 0
	oNameCompare = "SK_Pilot_"
	oNameCharCount = 9
	oNameNumCount = 10

End If


'Check all sketches in the model tree and compare the names to the
'name of the newly created sketch and if name exists increment the
'suffix by 1. Continue until the newly created sketch name is unique
'and can be used.
on error resume next
	For Each oSketchNameCheck As Sketch In oPartCompDef.Sketches
		If Left(oSketchNameCheck.Name,oNameCharCount) = oNameCompare Then
			Dim oSketchNumber As Integer = Val(Mid(oSketchNameCheck.Name, oNameNumCount, 2))			
	        While oSketchNameSuffix <= oSketchNumber
				oSketchNameSuffix = oSketchNameSuffix + 1
			End While
		End If	
	Next 
	
oSketch.Name = oNameCompare & oSketchNameSuffix

'QTY of holes and distance from front, left and right edges
oQty = 4
oFrontEdgeDistance = ThisApplication.MeasureTools.GetMinimumDistance(oFrontEdge.StartVertex, oFrontEdge.StopVertex)
oFromLeftEdge = FromLeftEdge * 2.54
oFromRightEdge = oFrontEdgeDistance - (FromRightEdge *2.54)
oFromFrontEdge = FromFrontEdge * 2.54
oPatternSpacing = (oFrontEdgeDistance - (oFromLeftEdge + oFromRightEdge)) / (oQty - 1)

Call oSketch.SketchLines.AddByTwoPoints(oTG.CreatePoint2d(oFromLeftEdge, oFromFrontEdge), oTG.CreatePoint2d(oFromRightEdge, oFromFrontEdge))
'Dim oSketchLine As SketchEntitiesEnumerator = oSketch.SketchLines.Item(1)
'Dim oLine as oSketchLines = oSketch.SketchLines.Item(1)

'create a sketch point to check if the coordinate system needs
'To be flipped so the circles fall inside the parent part.

OriginCheck()
	If (foundObjects.Count = 0) Then
		oSketch.NaturalAxisDirection = False
		OriginCheck()
		If (foundObjects.Count = 0) Then
			oSketch.NaturalAxisDirection = True
			oSketch.OriginPoint = oFrontEdge.StopVertex
			OriginCheck()
			If (foundObjects.Count = 0) Then
				oSketch.NaturalAxisDirection = False
				OriginCheck()
			End If
		End If
	End If
	
oSketchPoint.Delete


Dim oPoint As Point = oSketch.SketchToModelSpace(oTG.CreatePoint2d(oFromLeftEdge, oFromFrontEdge))
Dim oLinearPlacementDef As LinearHolePlacementDefinition = oPartCompDef.Features.HoleFeatures.CreateLinearPlacementDefinition(oFace, oLeftEdge, oFromLeftEdge, oFrontEdge, oFromFrontEdge, oPoint)
Call oPartCompDef.Features.HoleFeatures.AddDrilledByThroughAllExtent(oLinearPlacementDef, ".156in", kPositiveExtentDirection)

' Create the object collection.
Dim FeatureColl As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection

' Add the newly created hole feature to the collection.
Call FeatureColl.Add(oPartCompDef.Features.HoleFeatures.Item(1))

' Number of elements (X axis)
Dim NbrOfHolesLength As Integer
NbrOfHolesLength = 2

' Distance between elements.
Dim DistanceHolesLength As Double
DistanceHolesLength = 1

' Number of elements (Y axis)
Dim NbrOfHolesWidth As Integer
NbrOfHolesWidth = 1

' Distance between elements.
Dim DistanceHolesWidth As Double
DistanceHolesWidth = 1

' Create the pattern.

Call oPartCompDef.Features.RectangularPatternFeatures.Add(FeatureColl, oFrontEdge, True, NbrOfHolesLength, _
DistanceHolesLength * 2.54, PatternSpacingTypeEnum.kFitToPathLength, , , , , , , , , kIdentical)

'oSketch.Delete()

End Sub

Public Sub OriginCheck()
	oSketchPoint = oSketch.SketchPoints.Add(oTG.CreatePoint2d(oFromLeftEdge, oFromFrontEdge))
	SketchPointParentSketch = oSketchPoint.Parent
	vectorForCheck = SketchPointParentSketch.PlanarEntityGeometry.Normal
	pointToCheck = oSketchPoint.Geometry3d
	foundObjects = Nothing
	locationPoints = Nothing
	oPartCompDef.FindUsingRay(pointToCheck, vectorForCheck, .00001, foundObjects, locationPoints)
End Sub

End Class

 


Please accept as solution and give likes if applicable.

I am attaching my Upwork profile for specific queries.

Sergio Daniel Suarez
Mechanical Designer

| Upwork Profile | LinkedIn

0 Likes
Message 3 of 5

neodd70
Enthusiast
Enthusiast

@Sergio.D.Suárez for the sake of simplicity and to explain better what I am trying to accomplish i have reduced the code down to just what is needed to show where I am stuck.  If you look at the following code at about the halfway mark I create a line in the sketch that I want to use as the direction constraint for the rectangular pattern.  My issue is at the very bottom of the code where I create the pattern. Currently I use oFrontEdge as the FitToPath edge but what I want is to use line I created in the sketch as that will be able to be controlled with parameters.

Dim oPartDoc As Document = ThisDoc.Document
Dim oPartCompDef As PartComponentDefinition = ThisApplication.ActiveDocument.ComponentDefinition
Dim oTG As TransientGeometry = ThisApplication.TransientGeometry

'Make shortest value the Thickness, rounded to 4 places

'Select Face and Edges to dimension sketch circles from
Dim oFace As Face = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFaceFilter, "Select Surface to Place Holes")
Dim oFrontEdge As Edge = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartEdgeFilter, "Select Front Edge of Pattern")
Dim oLeftEdge As Edge = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartEdgeFilter, "Select Left Edge of Pattern")
Dim oRightEdge As Edge = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartEdgeFilter, "Select Right Edge of Pattern")

'Add Sketch and set sketch origin point to the startVertex of the FrontEdge

Dim oSketch As PlanarSketch = oPartCompDef.Sketches.Add(oFace, False)
oSketch.AxisEntity = oFrontEdge
oSketch.OriginPoint = oFrontEdge.StartVertex
oSketch.NaturalAxisDirection = False

'Create a line in the sketch to constrain the rectangular pattern direction to
Call oSketch.SketchLines.AddByTwoPoints(oTG.CreatePoint2d(10, 2), oTG.CreatePoint2d(50, 2))
'Dim oSketchLine As SketchEntitiesEnumerator = oSketch.SketchLines.Item(1)
'Dim oLine as oSketchLines = oSketch.SketchLines.Item(1)

Dim oPoint As Point = oSketch.SketchToModelSpace(oTG.CreatePoint2d(10, 2))
Dim oLinearPlacementDef As LinearHolePlacementDefinition = oPartCompDef.Features.HoleFeatures.CreateLinearPlacementDefinition(oFace, oLeftEdge, 10, oFrontEdge, 2, oPoint)
Call oPartCompDef.Features.HoleFeatures.AddDrilledByThroughAllExtent(oLinearPlacementDef, ".156in", kPositiveExtentDirection)

' Create the object collection.
Dim FeatureColl As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection

' Add the newly created hole feature to the collection.
Call FeatureColl.Add(oPartCompDef.Features.HoleFeatures.Item(1))

' Number of elements (X axis)
Dim NbrOfHolesLength As Integer = 2

' Distance between elements.
Dim DistanceHolesLength As Double = 1

' Create the pattern.
Call oPartCompDef.Features.RectangularPatternFeatures.Add(FeatureColl, oFrontEdge, True, NbrOfHolesLength, _
DistanceHolesLength * 2.54, PatternSpacingTypeEnum.kFitToPathLength, , , , , , , , , kIdentical)
0 Likes
Message 4 of 5

neodd70
Enthusiast
Enthusiast
Accepted solution

I was eventually able to figure out where my error was. To use a line from a sketch as the FitToPathLength property of a rectangular pattern you must first make a "Path" from the SketchLine. 

Dim oPatternPath As Object = oPartCompDef.Features.CreatePath(oSketch.SketchLines.Item(1))

I guess the clue was in the PatternSpacingTypeEnum Name, kFitTo"PATH"Length

0 Likes
Message 5 of 5

uo9K5H5
Contributor
Contributor

Hello,

I have a similar problem and I think I can use your code. I would like to use same princip for pattering the points on a Shrinkwrap that is not linear.  I would like to choose face and than all edges on which I would like to pattern points. I am attaching a picture for better perception. 

Screenshot 2021-07-22 134514.pngScreenshot 2021-07-22 134611.png

 

0 Likes