Thanks @Curtis_Waguespack for constraints code,
Hi @chrisw01a,
The following iLogic code handles both horizontal and vertical lines.
Sub Main
'[ edit these variables as needed
sSketchName = "Flat Pattern Sketch"
sHoleName = "Locator Holes"
oOffset = 0.1 'defines offset from edge of flat pattern
oDiameter = 0.125 'defines hole diameter
']
' a reference to the active document.
Dim oPartDoc As PartDocument
oPartDoc = ThisApplication.ActiveDocument
'verify document type is sheet metal
If oPartDoc.ComponentDefinition.Type <> 150995200 Then
MessageBox.Show("File is not a sheet metal part.", "iLogic")
Exit Sub
End If
Dim oCompDef As SheetMetalComponentDefinition
oCompDef = oPartDoc.ComponentDefinition
' Check to make sure a flat pattern is open.
If Not TypeOf ThisApplication.ActiveEditObject Is FlatPattern Then
Try
If oCompDef.HasFlatPattern = False Then
oCompDef.Unfold
Else
oCompDef.FlatPattern.Edit
End If
Catch
MessageBox.Show("Error editting the flat pattern.", "iLogic")
End Try
End If
' a reference to the active flat pattern.
Dim oFlatPattern As FlatPattern
oFlatPattern = ThisApplication.ActiveEditObject
'clean up existing holes
Dim oHole As HoleFeature
For Each oHole In oFlatPattern.Features.HoleFeatures
oHole.Delete
Next
Dim oFace As Face
'oFace = oFlatPattern.BottomFace
oFace = oFlatPattern.TopFace
Dim oSketch As PlanarSketch
'clean up existing sketch
For Each oSketch In oFlatPattern.Sketches
If oSketch.Name = sSketchName Then
oSketch.Delete
End If
Next
' Create a new sketch. The second argument specifies to include/not include
' the edges of the face in the sketch.
oSketch = oFlatPattern.Sketches.Add(oFace, False)
' Change the name.
oSketch.Name = sSketchName
'Dim oPoint As Point2d
Dim oSketchPoint As SketchPoint
oOffset = oOffset * 2.5400013716 'converts cm to inches
Dim oEdges As Edges
' Create a new object collection for the hole center points.
oHoleCenters = ThisApplication.TransientObjects.CreateObjectCollection
' Get all Bend UP edges
'where true = top face
oEdges = _
oFlatPattern.GetEdgesOfType( _
FlatPatternEdgeTypeEnum.kBendUpFlatPatternEdge, True)
Call Create_SketchPoints(oSketch, oEdges, oHoleCenters, oOffset)
' Get all Bend Down edges
'where true = top face
oEdges = _
oFlatPattern.GetEdgesOfType( _
FlatPatternEdgeTypeEnum.kBendDownFlatPatternEdge, True)
Call Create_SketchPoints(oSketch, oEdges, oHoleCenters, oOffset )
' Create the hole feature.
oHole = oFlatPattern.Features.HoleFeatures.AddDrilledByThroughAllExtent( _
oHoleCenters, oDiameter * 2.5400013716 , kPositiveExtentDirection)
oHole.Name = sHoleName
End Sub
Sub Create_SketchPoints (oSketch As Sketch, oEdges As Edges, oHoleCenters As ObjectCollection, oOffset As Double)
For Each oEdge In oEdges
Dim line As SketchLine = oSketch.AddByProjectingEntity(oEdge)
Dim startPt As Point2d
startPt = line.StartSketchPoint.Geometry
Dim endPt As Point2d
endPt = line.EndSketchPoint.Geometry
Dim startSkPt As SketchPoint
Dim endSkPt As SketchPoint
If Round(startPt.Y, 4) = Round(endPt.Y,4) Then
If startPt.X < endPt.X Then
startPt.X = startPt.X + oOffset
endPt.X = endPt.X - oOffset
Else
startPt.X = startPt.X - oOffset
endPt.X = endPt.X + oOffset
End If
startSkPt = oSketch.SketchPoints.Add(startPt, True)
oSketch.GeometricConstraints.AddCoincident(startSkPt, line)
oSketch.DimensionConstraints.AddTwoPointDistance _
(startSkPt, line.StartSketchPoint, DimensionOrientationEnum.kHorizontalDim, startPt)
endSkPt = oSketch.SketchPoints.Add(endPt, True)
oSketch.GeometricConstraints.AddCoincident(endSkPt, line)
oSketch.DimensionConstraints.AddTwoPointDistance _
(endSkPt, line.EndSketchPoint, DimensionOrientationEnum.kHorizontalDim, endPt)
Else if Round(startPt.X, 4) = Round(endPt.X,4) Then
If startPt.Y < endPt.Y Then
startPt.Y = startPt.Y + oOffset
endPt.Y = endPt.Y - oOffset
Else
startPt.Y = startPt.Y - oOffset
endPt.Y = endPt.Y + oOffset
End If
startSkPt = oSketch.SketchPoints.Add(startPt, True)
oSketch.GeometricConstraints.AddCoincident(startSkPt, line)
oSketch.DimensionConstraints.AddTwoPointDistance _
(startSkPt, line.StartSketchPoint, DimensionOrientationEnum.kVerticalDim, startPt)
endSkPt = oSketch.SketchPoints.Add(endPt, True)
oSketch.GeometricConstraints.AddCoincident(endSkPt, line)
oSketch.DimensionConstraints.AddTwoPointDistance _
(endSkPt, line.EndSketchPoint, DimensionOrientationEnum.kVerticalDim, endPt)
End If
oHoleCenters.Add(startSkPt)
oHoleCenters.Add(endSkPt)
Next
End Sub
Please feel free to contact if there is any doubt.
If solves your problem, click on "Accept as solution" / give a "Kudo".
Thanks and regards,
CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network