Message 1 of 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I have code that takes the part number property and uses it to put a mark on sheet metal parts.
You select a face of the part, it creates the mark, copies the sketch to the flat pattern, and deletes the folded model sketch. You can still edit the part number and it will update on the flat pattern level. The issue I have is making the mark feature within flat pattern. The sketch is made, but I think there's some issue with selecting the sketch objects.
This error comes up
Error on line 103 in rule: Mark Part Number V6, in document: Part2
Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))
''' Mark Part Number is meant to add the part number property to the sheet metal flat pattern level.
Sub Main()
' ================= Determine text size based on thickness ========================
Dim oTypes_Text As String
If SheetMetal.GetActiveStyle.Contains("14ga")
oTypes_Text = ".25"
Else If SheetMetal.GetActiveStyle.Contains("12ga")
oTypes_Text = ".25"
Else If SheetMetal.GetActiveStyle.Contains("3/16in")
oTypes_Text = ".25"
Else If SheetMetal.GetActiveStyle.Contains("1/4in")
oTypes_Text = ".3125"
Else If SheetMetal.GetActiveStyle.Contains("3/8in")
Exit Sub
Else If SheetMetal.GetActiveStyle.Contains("1/2in")
Exit Sub
Else If SheetMetal.GetActiveStyle.Contains("5/8in")
Exit Sub
Else If SheetMetal.GetActiveStyle.Contains("3/4in")
Exit Sub
End If
' =================
' Reference the active document.
Dim oPartDoc As PartDocument = ThisApplication.ActiveDocument
Dim oSheetMetalCompDef As SheetMetalComponentDefinition = oPartDoc.ComponentDefinition
' Set a reference to transient geometry.
Dim oTransGeom As TransientGeometry = ThisApplication.TransientGeometry
' ================= Get Part Number from iProperties ==============================
Dim invPartNumberProperty = oPartDoc.PropertySets.Item("Design Tracking Properties").Item("Part Number")
Dim PartNumber As String = invPartNumberProperty.Value
' ================= Pick a Face and Put a Point in the Center =====================
Dim oFace As Face = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFaceFilter, "Select Surface to Place Text On")
oEdgeLoop = oFace.EdgeLoops(1)
oMinPt = oEdgeLoop.RangeBox.MinPoint
oMaxPt = oEdgeLoop.RangeBox.MaxPoint
CenterPt = ThisApplication.TransientGeometry.CreatePoint((oMaxPt.X + oMinPt.X) / 2#, (oMaxPt.Y + oMinPt.Y) / 2#, (oMaxPt.Z + oMinPt.Z) / 2#)
oSketch = oSheetMetalCompDef.Sketches.Add(oFace)
oSketch.Name = "Part Number Sketch"
oTextPt = oSketch.ModelToSketchSpace(CenterPt)
' ================= Adding the Textbox ====================================
Dim oSketchText As Inventor.TextBox
oSketchText = oSketch.TextBoxes.AddFitted(oTextPt, sText)
oSketchText.VerticalJustification = VerticalTextAlignmentEnum.kAlignTextMiddle
oSketchText.HorizontalJustification = HorizontalTextAlignmentEnum.kAlignTextCenter
Dim oFontSize As String = (oTypes_Text * 2.54).ToString("F2") ' Convert to cm
oSketchText.FormattedText = "<StyleOverride FontSize='" & oFontSize & "'> <Property Document='' PropertySet='Design Tracking Properties' Property='Part Number' FormatID='' PropertyID=''>PART NUMBER</Property> </StyleOverride>"
' ================= Unfold the pattern and delete to part sketch ===========
' Get the flat pattern.
oSheetMetalCompDef.Unfold
Dim oFlatPattern As FlatPattern = oSheetMetalCompDef.FlatPattern
oSketch.CopyToFlatPattern = True
oSketch.Delete()
Dim fName As String = "Part Number Sketch:Copy"
Dim fSketch As Sketch = Nothing
For Each fSketch In oFlatPattern.Sketches
If fSketch.Name = fName Then
fSketch.Name = "Part Number Sketch"
Exit For
End If
Next
' ================= Create Mark Feature ==========================================
' Validate Mark Style availability
Dim oMarkStyle As MarkStyle
If oPartDoc.MarkStyles.Count >= 2 Then
oMarkStyle = oPartDoc.MarkStyles.Item(2)
Else
MessageBox.Show("Mark Style not found.", "Error")
Exit Sub
End If
Dim oSketchObjects As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection()
oSketchObjects.Add(oSketchText)
Dim oMarkFeatures As MarkFeatures = oFlatPattern.Features.MarkFeatures
Dim oMarkDef As MarkDefinition = oMarkFeatures.CreateMarkDefinition(oSketchObjects, oMarkStyle)
Dim oMark As MarkFeature
oMark = oMarkFeatures.Add(oMarkDef)
oMark.Name = "Part Number"
End Sub
Solved! Go to Solution.