Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

flat pattern extents when part not fully created

9 REPLIES 9
SOLVED
Reply
Message 1 of 10
emanuel.c
595 Views, 9 Replies

flat pattern extents when part not fully created

I have this piece of code (part of a function which returns "getstock") to calculate the area of the flat patterns of sheet metal, with an additional kerf, the thickness dimension. It works well, however I need to add some error exits and I'm not sure what exactly should be done. The code is triggered on Save. Basically, if the sheet metal is not yet created and I wish to save the part for the sake of the sketch it will trigger errors because there are no flat pattern extents. I could let the error be and keep on, but sometimes I may create sketches on sheet metal to drive other parts in the model.

 

Do I need to go with searching to see if those parameters (flat pattern extends) exist? I tried the If IsNot but that doesn't seem to work. What's the way to do it? Thank you much!

 

Dim smlength = SheetMetal.FlatExtentsLength 'sheet metal flat pattern length
	Dim smwidth = SheetMetal.FlatExtentsWidth 'sheet metal flat pattern width
	Dim AreaPronest As Double = (smlength + Parameter("Thickness")) * (smwidth + Parameter("Thickness")) / 144
	Dim oSMthk As Double = Parameter("Thickness")
	If Parameter("Thickness") >= 0.1875 Then
		getstock ="Plate, " & Format(oSMthk, "0.000") & " thk x " & Format(AreaPronest, "0.00") & " ft^2"
	Else
		getstock ="Sheet Metal, " & Format(oSMthk, "0.000") & " thk x " & Format(AreaPronest, "0.00") & " ft^2"
	End If

  

9 REPLIES 9
Message 2 of 10
llorden4
in reply to: emanuel.c

Might try a change with how you're declaring & defining the variable.  There's also a Boolean to lookup if there's a current flat sheet metal pattern you can look up and use, I don't recall it off the top of my head.

 

Dim smlength as Single 'or Double
Dim smwidth as Single 'or Double
Try
smlength = SheetMetal.FlatExtentsLength
smwidth = SheetMetal.FlatExtentsWidth
Catch
End Try
 

   

Autodesk Inventor Certified Professional
Message 3 of 10
WCrihfield
in reply to: emanuel.c

Since this is only part of a larger function, and I also don't know what the rest of the main may look like, the simplest thing I can think of it to just put that bit of code within a Try...Catch...End Try block of code, so that it will 'handle' the error the way you choose to.  You can put whatever you want in the Catch portion of that block of code, and it will do that when the Try portion fails.  I usually include a MsgBox with something specific, and maybe error details, then usually a line to either exit the rule, or exit the Sub/Function it is in, but that's up to you.  Here's what it might look like:

Try
	Dim smlength = SheetMetal.FlatExtentsLength 'sheet metal flat pattern length
	Dim smwidth = SheetMetal.FlatExtentsWidth 'sheet metal flat pattern width
	Dim AreaPronest As Double = (smlength + Parameter("Thickness")) * (smwidth + Parameter("Thickness")) / 144
	Dim oSMthk As Double = Parameter("Thickness")
	If Parameter("Thickness") >= 0.1875 Then
		getstock ="Plate, " & Format(oSMthk, "0.000") & " thk x " & Format(AreaPronest, "0.00") & " ft^2"
	Else
		getstock ="Sheet Metal, " & Format(oSMthk, "0.000") & " thk x " & Format(AreaPronest, "0.00") & " ft^2"
	End If
Catch oEx As Exception
	MsgBox("Failed to get SheetMetal.FlatExtents size." & vbCrLf & _
	oEx.Message & vbCrLf & oEx.StackTrace, vbExclamation, "iLogic")
	Exit Function
End Try

 ...but you could just comment out the stuff in the Catch portion, if that doesn't suit your needs.

 

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.

If you want and have time, I would appreciate your Vote(s) for My IDEAS :light_bulb:or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 4 of 10
emanuel.c
in reply to: WCrihfield

Hmm, I forgot about the Try Catch, thank you. However I still get an error: Conversion from string "iLogic" to type "Integer" is not valid. Here is the code if there's something else related to it, though I suspected it was because there was no metal to flat bend yet, since it works OK otherwise. I guess if I'd be using VB I could run it line by line to see where the issue is... I'm still working on the code and it's kind of pieced together as best I knew how.

 

Basically it detects if parameter "FL" exists (which is not needed for flat metal shapes, so it shouldn't be created for sheet metal parts - still need to work on that) then if iProperty "Stock Size" exists it populates that according to iProperty "Product".

 

Thank you for helping out!

 

' Code to write "Stock Size" iProperty

Sub Main()
	
Dim invdoc As Document
invdoc = ThisDoc.Document

'Function GetUserParams returns UserParameters Collection
oUserParams = GetUserParams(invdoc)
 
'Function FindParameter returns the looked for parameter
paraFL = FindParameter(invdoc, "FL")
  
If paraFL IsNot Nothing Then 
	'MessageBox.Show("Parameter does exist", "Title")
Else
	'MessageBox.Show("Parameter does not exist", "Title")
	paraFL = oUserParams.AddByValue("FL", 0, UnitsTypeEnum.kInchLengthUnits)
End If

Dim oPart As PartDocument = ThisDoc.Document
	Dim oSS As String = StockSize(oPart)
	Logger.Info(oSS)

End Sub

'See if parameter FL exists
Public Function FindParameter(invdoc As Inventor.Document, strParameter As String) As Parameter

Dim cd As ComponentDefinition = Nothing
Select Case invdoc.DocumentType
    Case Is = Inventor.DocumentTypeEnum.kAssemblyDocumentObject
		'MessageBox.Show("Is a Assembly", "Title")
        Dim docAssembly As Inventor.AssemblyDocument = invdoc
        cd = docAssembly.ComponentDefinition
    Case Is = Inventor.DocumentTypeEnum.kPartDocumentObject
		'MessageBox.Show("Is a Part", "Title")
        Dim docPart As Inventor.PartDocument = invdoc
        cd = docPart.ComponentDefinition
End Select
If cd IsNot Nothing Then
    For Each param As Inventor.Parameter In cd.Parameters
        If param.Name = strParameter Then
            Return param
		End If
    Next
End If
 Return Nothing
End Function

'Get user parameters collection
Public Function GetUserParams(invdoc As Inventor.Document) As UserParameters
	
	Dim cd As ComponentDefinition = Nothing
    Select Case invdoc.DocumentType
        Case Is = Inventor.DocumentTypeEnum.kAssemblyDocumentObject
			'MessageBox.Show("Is a Assembly", "Title")
            Dim docAssembly As Inventor.AssemblyDocument = invdoc
            cd = docAssembly.ComponentDefinition
        Case Is = Inventor.DocumentTypeEnum.kPartDocumentObject
			'MessageBox.Show("Is a Part", "Title")
            Dim docPart As Inventor.PartDocument = invdoc
            cd = docPart.ComponentDefinition
    End Select
   
   If cd IsNot Nothing Then

			Dim oParams As Parameters
			oParams = cd.Parameters
			
			Dim oUserParams As UserParameters
			oUserParams = oParams.UserParameters
             Return oUserParams
    End If
	 Return Nothing
End Function



Private Function StockSize(part As PartDocument) As String
	Dim oStock As String = getstock(part)
	Dim propertyName As String = "STOCK SIZE"
	Dim PropertyValue As String = oStock
	
	Dim oCProps As PropertySet = part.PropertySets.Item("Inventor User Defined Properties")
	Dim oSSProp As Inventor.Property
	Dim oExists As Boolean = False
	For Each oCProp As Inventor.Property In oCProps
		If oCProp.Name = propertyName Then
			oSSProp = oCProp
			oSSProp.Value = PropertyValue
			oExists = True
		End If
	Next
	If oExists = False Then
		oSSProp = oCProps.Add(propertyName, propertyName)
	End If

	StockSize = oSSProp.Value
End Function

'Write stock size for each part shape
Private Function getstock(part As PartDocument) As String

Dim oFL As Double = Parameter("FL")
	
Select Case iProperties.Value("Inventor User Defined Properties", "Product")

	Case "01 Sheet Metal"
	
		Try
			Dim smlength As Double = SheetMetal.FlatExtentsLength 'sheet metal flat pattern length
			Dim smwidth as Double = SheetMetal.FlatExtentsWidth 'sheet metal flat pattern width
			Dim AreaPronest As Double = (smlength + Parameter("Thickness")) * (smwidth + Parameter("Thickness")) / 144
			Dim oSMthk As Double = Parameter("Thickness")
			If Parameter("Thickness") >= 0.1875 Then
				getstock ="Plate, " & Format(oSMthk, "0.000") & " thk x " & Format(AreaPronest, "0.00") & " ft^2"
			Else
				getstock ="Sheet Metal, " & Format(oSMthk, "0.000") & " thk x " & Format(AreaPronest, "0.00") & " ft^2"
			End If
		Catch oEx As Exception
			MsgBox("Failed to get SheetMetal.FlatExtents size.", "iLogic")
		End Try
				
	Case "02 Pipe"
		Dim oPipeD As Double = Parameter("G_D_Nom")
		oL = FormatAsFraction(oPipeD)
		getstock = "Pipe, " & oL & " sch." & Parameter("Schedule") & " x " & Format(oFL, "0.00")
		
	Case "03 Angle"
		Dim Athk As Double = Parameter("G_T")
		Dim AH As Double = Parameter("G_H")
		Dim AW As Double = Parameter("G_W")
		getstock = "L " & Format(AW, "0.00") & " x " & Format(AH, "0.00") & " x " & Format(Athk, "0.00") & " x " & Format(oFL, "0.00")

	Case Else
		getstock = "not defined"
	End Select

End Function

 

Message 5 of 10
Michael.Navara
in reply to: emanuel.c

You can use direct API functions for this.

This sample expects the sheetmetal part is the active document and returns flatpattern dimensions in internal units [cm].

Dim part As PartDocument = ThisDoc.Document
Dim smCompDef As SheetMetalComponentDefinition = part.ComponentDefinition

Dim length As Double
Dim width As Double
If smCompDef.HasFlatPattern Then
	width = smCompDef.FlatPattern.Width
	length = smCompDef.FlatPattern.Length
End If

 

Message 6 of 10
emanuel.c
in reply to: Michael.Navara

Thank you Michael, I didn't know it had the API.

 

I still get errors, only when there isn't a flat pattern available and can't figure it out... Unspecified Error (Exception from HRESULT: 0x80004005 (E_FAIL)).

 

' Code to write "Stock Size" iProperty

Sub Main()
	
Dim invdoc As Document
invdoc = ThisDoc.Document

'Function GetUserParams returns UserParameters Collection
oUserParams = GetUserParams(invdoc)
 
'Function FindParameter returns the looked for parameter
paraFL = FindParameter(invdoc, "FL")
  
If paraFL IsNot Nothing Then 
	'MessageBox.Show("Parameter does exist", "Title")
Else
	'MessageBox.Show("Parameter does not exist", "Title")
	paraFL = oUserParams.AddByValue("FL", 0, UnitsTypeEnum.kInchLengthUnits)
End If

Dim oPart As PartDocument = ThisDoc.Document
	Dim oSS As String = StockSize(oPart)
	Logger.Info(oSS)

End Sub

'See if parameter FL exists
Public Function FindParameter(invdoc As Inventor.Document, strParameter As String) As Parameter

Dim cd As ComponentDefinition = Nothing
Select Case invdoc.DocumentType
    Case Is = Inventor.DocumentTypeEnum.kAssemblyDocumentObject
		'MessageBox.Show("Is a Assembly", "Title")
        Dim docAssembly As Inventor.AssemblyDocument = invdoc
        cd = docAssembly.ComponentDefinition
    Case Is = Inventor.DocumentTypeEnum.kPartDocumentObject
		'MessageBox.Show("Is a Part", "Title")
        Dim docPart As Inventor.PartDocument = invdoc
        cd = docPart.ComponentDefinition
End Select
If cd IsNot Nothing Then
    For Each param As Inventor.Parameter In cd.Parameters
        If param.Name = strParameter Then
            Return param
		End If
    Next
End If
 Return Nothing
End Function

'Get user parameters collection
Public Function GetUserParams(invdoc As Inventor.Document) As UserParameters
	
	Dim cd As ComponentDefinition = Nothing
    Select Case invdoc.DocumentType
        Case Is = Inventor.DocumentTypeEnum.kAssemblyDocumentObject
            Dim docAssembly As Inventor.AssemblyDocument = invdoc
            cd = docAssembly.ComponentDefinition
        Case Is = Inventor.DocumentTypeEnum.kPartDocumentObject
            Dim docPart As Inventor.PartDocument = invdoc
            cd = docPart.ComponentDefinition
    End Select
   
   If cd IsNot Nothing Then
Dim oParams As Parameters oParams = cd.Parameters Dim oUserParams As UserParameters oUserParams = oParams.UserParameters Return oUserParams End If Return Nothing End Function
Private Function StockSize(part As PartDocument) As String Dim oStock As String = getstock(part) Dim propertyName As String = "STOCK SIZE" Dim PropertyValue As String = oStock Dim oCProps As PropertySet = part.PropertySets.Item("Inventor User Defined Properties") Dim oSSProp As Inventor.Property Dim oExists As Boolean = False For Each oCProp As Inventor.Property In oCProps If oCProp.Name = propertyName Then oSSProp = oCProp oSSProp.Value = PropertyValue oExists = True End If Next If oExists = False Then oSSProp = oCProps.Add(propertyName, propertyName) End If StockSize = oSSProp.Value End Function 'Write stock size for each part shape Private Function getstock(part As PartDocument) As String Dim oFL As Double = Parameter("FL") Select Case iProperties.Value("Inventor User Defined Properties", "Product") Case "01 Sheet Metal" Dim opart As PartDocument = ThisDoc.Document Dim smCompDef As SheetMetalComponentDefinition = opart.ComponentDefinition Dim smlength As Double = SheetMetal.FlatExtentsLength 'sheet metal flat pattern length Dim smwidth As Double = SheetMetal.FlatExtentsWidth 'sheet metal flat pattern width Dim AreaPronest As Double = (smlength + Parameter("Thickness")) * (smwidth + Parameter("Thickness")) / 144 Dim oSMthk As Double = Parameter("Thickness") If smCompDef.HasFlatPattern Then If Parameter("Thickness") >= 0.1875 Then getstock ="Plate, " & Format(oSMthk, "0.000") & " thk x " & Format(AreaPronest, "0.00") & " ft^2" Else getstock ="Sheet Metal, " & Format(oSMthk, "0.000") & " thk x " & Format(AreaPronest, "0.00") & " ft^2" End If End If Case "02 Pipe" Dim oPipeD As Double = Parameter("G_D_Nom") oL = FormatAsFraction(oPipeD) getstock = "Pipe, " & oL & " sch." & Parameter("Schedule") & " x " & Format(oFL, "0.00")

End Select
End Function

  

Message 7 of 10
WCrihfield
in reply to: emanuel.c

Hi @emanuel.c.  Within your 'getstock' function, you are checking 'smCompDef.HasFlatPattern' several lines after you have already attempted to extract data from the FlatPattern with the 'SheetMetal.FlatExtentsLength' & 'SheetMetal.FlatExtentsWidth' lines.  This is obviously not proper logic.  You should be checking for the existence of a FlatPattern before trying to use it or extract data from it.  Then, if not found, either include some code (like Unfold) to create the FlatPattern before trying to access it, or avoid attempting to access it at all.  Because when you try to access/use it, and it doesn't exist yet, it will throw an error.  Just one observation.

 

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 8 of 10
emanuel.c
in reply to: emanuel.c

Yes, thanks Chris. I noticed that later and corrected it, then it worked great. I also added a Try - Catch in there because I needed it to try to create a flat pattern in case a sheet metal body was built but flat pattern wasn't made yet.

 

Dim opart As PartDocument = ThisDoc.Document
Dim smCompDef As SheetMetalComponentDefinition = opart.ComponentDefinition
Dim smlength As Double
Dim smwidth As Double
Dim oSMthk As Double = Parameter("Thickness")
If smCompDef.HasFlatPattern Then			
	smlength = (smCompDef.FlatPattern.Length + oSMthk) / 2.54
	smwidth = (smCompDef.FlatPattern.Width + oSMthk) / 2.54
	Else
	Try
		smCompDef.Unfold()
		smlength = (smCompDef.FlatPattern.Length + oSMthk) / 2.54
		smwidth = (smCompDef.FlatPattern.Width + oSMthk) / 2.54
	Catch
	End Try
	End If

	Dim AreaPronest As Double = smlength * smwidth / 144
		
	If Parameter("Thickness") >= 0.1875 Then
		getstock ="Plate, " & Format(oSMthk, "0.000") & " thk x " & Format(AreaPronest, "0.00") & " ft^2"
	Else
		getstock ="Sheet Metal, " & Format(oSMthk, "0.000") & " thk x " & Format(AreaPronest, "0.00") & " ft^2"
	End If

 

Message 9 of 10
emanuel.c
in reply to: emanuel.c

Wow, I can't believe it can be this troublesome... The issue now is, if a flat pattern already exists but the part size may have changed it doesn't update the smCompDef.FlatPattern Length and Width...

Message 10 of 10
emanuel.c
in reply to: emanuel.c

Hmm I guess I can use the brackets () to update:

 

Dim opart As PartDocument = ThisDoc.Document()

smlength = (smCompDef.FlatPattern.Length() + oSMthk) / 2.54
smwidth = (smCompDef.FlatPattern.Width() + oSMthk) / 2.54

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Technology Administrators


Autodesk Design & Make Report