Giving Sheet Metal a Mark in the Flat Pattern Level

Giving Sheet Metal a Mark in the Flat Pattern Level

izaakHNEB8
Participant Participant
412 Views
2 Replies
Message 1 of 3

Giving Sheet Metal a Mark in the Flat Pattern Level

izaakHNEB8
Participant
Participant

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

 

0 Likes
Accepted solutions (1)
413 Views
2 Replies
Replies (2)
Message 2 of 3

marcin_otręba
Advisor
Advisor
Accepted solution

Hi,

 

One error is in formatted text string refering to inventor help:

Inventor 2025 Help | XML Tags for FormattedText | Autodesk

 

it should be something like:

oSketchText.FormattedText = "<StyleOverride FontSize='" & oFontSize & "'><Property Document='model' FormatID='{32853F0F-3444-11D1-9E93-0060B03C1CA6}' PropertyID='5'>Part Number</Property></StyleOverride>"

 

next you need to change oMarkDef reffering to inventors help sample :

Inventor 2025 Help | Mark feature creation sample | Autodesk

 

to :

Dim oGeometries As ObjectCollection
     oGeometries = ThisApplication.TransientObjects.CreateObjectCollection
	oGeometries.Add(oFlatPattern.Sketches(1).TextBoxes(1)) ' assuming that it will be first sketch
	'oFlatPattern.Edit
    Dim oMarkFeatures As MarkFeatures = oFlatPattern.Features.MarkFeatures
    Dim oMarkDef As MarkDefinition = oMarkFeatures.CreateMarkDefinition(oGeometries, oMarkStyle)

 

and it will work. Of course it can be simplified like why to create sketch in model space and copy it to flatpattern? 

Hi, maybe you want to check my apps:


DrawingTools   View&ColoringTools   MRUFolders

0 Likes
Message 3 of 3

izaakHNEB8
Participant
Participant

Thank you for the reply. I have found a solution using the example you provided. The XML tags is a great help, I didn't know what I was doing and stumbled upon something that worked. As for the Mark feature I followed along with what you provided and now its working how I wanted it to. To answer your question I create the sketch first in the model view because I am using the pick feature to select the face which the part number will be on. But I don't want it to be on the model view since if I derive the part and give it a new part number then there will be conflicting information. So the solution is to have the parr number in the flat pattern since that doesn't carry over with a derive. Here's the code I have that works:

 

''' 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='model' FormatID='{32853F0F-3444-11D1-9E93-0060B03C1CA6}' PropertyID='5'>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
	oSketchObjects = ThisApplication.TransientObjects.CreateObjectCollection
    oSketchObjects.Add(oFlatPattern.Sketches("Part Number Sketch").TextBoxes(1))

    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

 

0 Likes