ilogic - delete flat patern

ilogic - delete flat patern

tegstette
Advocate Advocate
2,276 Views
13 Replies
Message 1 of 14

ilogic - delete flat patern

tegstette
Advocate
Advocate

Hi

 

I am trying to make a rule that runs through my iam and delete all flat patterns on sheet metal parts

Can someone help me out with this rule.

Here is what I have:

 

 

'define the active assembly
Dim oAssyDoc As AssemblyDocument
oAssyDoc = ThisApplication.ActiveDocument  
'	MessageBox.Show("Message1", "Title")

Dim oDoc As Inventor.Document


'Check all referenced docs 
For Each oDoc In oAssyDoc.AllReferencedDocuments
	MessageBox.Show("Message2", "Title")
	
	'Dim oDoc As Inventor.Document
	Dim smpCompDef As Inventor.SheetMetalComponentDefinition
	
	MessageBox.Show("Message3", "Title")

	If (smpCompDef.HasFlatPattern = True) Then
			Try
				MessageBox.Show("Message4", "Title")
					smpCompDef.Delete()
				Catch	
				MessageBox.Show("Message5", "Title")
			End Try	
	End If    
Next

'update the files
InventorVb.DocumentUpdate()
Best regards
TG

Autodesk Inventor/Vault Professional 2021
0 Likes
Accepted solutions (1)
2,277 Views
13 Replies
Replies (13)
Message 2 of 14

Owner2229
Advisor
Advisor

Hi, try this:

 

' Define the active assembly
Dim oAssyDoc As AssemblyDocument
oAssyDoc = ThisApplication.ActiveDocument  
' Check all referenced docs For Each oDoc As Document In oAssyDoc.AllReferencedDocuments ' Get sheet metal component definition
Dim oSMCD As Inventor.SheetMetalComponentDefinition
' Check if it has flatpattern If (oSMCD.HasFlatPattern = True) Then ' Atempt to delete it
Try oSMCD.FlatPattern.Delete Catch End Try End If Next ' Update the files InventorVb.DocumentUpdate()
Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 3 of 14

tegstette
Advocate
Advocate

I ran it from an iam but got this error

 

Object reference not set to an instance of an object.

 

 

(By the way: Can the rule be run from both directly on the sheet metal and from iam?)

 

Best regards
TG

Autodesk Inventor/Vault Professional 2021
0 Likes
Message 4 of 14

Owner2229
Advisor
Advisor

Hi, there was one line missing about the component definition.

Now it will work in both assembly and sheetmetal part.

 

' Check the document type
If ThisApplication.ActiveDocument.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
    ' Define the active assembly
    Dim oAssyDoc As AssemblyDocument
    oAssyDoc = ThisApplication.ActiveDocument
    ' Check all referenced docs 
    For Each oDoc As Document In oAssyDoc.AllReferencedDocuments
        ' Check if the part is sheet metal
        If oDoc.DocumentSubType.DocumentSubTypeID = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then
            ' Get sheet metal component definition
            Dim oSMCD As Inventor.SheetMetalComponentDefinition
	    oSMCD = oDoc.ComponentDefinition
            ' Check If it has flatpattern
            If oSMCD.HasFlatPattern = True Then
                ' Atempt to delete it
                Try
                    oSMCD.FlatPattern.Delete
                Catch
                End Try	
            End If
        End If
    Next
ElseIf ThisApplication.ActiveDocument.DocumentSubType.DocumentSubTypeID = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then
    ' Get the sheetmetal component definition
Dim oSMCD As Inventor.SheetMetalComponentDefinition oSMCD = ThisApplication.ActiveDocument.ComponentDefinition ' Check If it has flatpattern If oSMCD.HasFlatPattern = True Then ' Atempt to delete it Try oSMCD.FlatPattern.Delete Catch End Try End If End If ' Update the files InventorVb.DocumentUpdate() 

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 5 of 14

tegstette
Advocate
Advocate

Hi

 

Now I got this error:

 

Error on Line 23 : End of statement expected

 

in this line:

 

ElseIf ThisApplication.ActiveDocument.DocumentSubType.Doc​umentSubTypeID = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then

 

Best regards
TG

Autodesk Inventor/Vault Professional 2021
0 Likes
Message 6 of 14

Owner2229
Advisor
Advisor
Accepted solution

Hi, put a space between "Else" and "If", so it would be:

Else If ThisApplication.ActiveDocument.DocumentSubType.Doc​umentSubTypeID = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then
Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 7 of 14

tegstette
Advocate
Advocate

I still get the same error... (tried both SM.ipt and iam)

Best regards
TG

Autodesk Inventor/Vault Professional 2021
0 Likes
Message 8 of 14

tegstette
Advocate
Advocate

I pasted you line from your last post into my rule and now it works Smiley Happy

 

Thank you! Works greatSmiley Happy

Best regards
TG

Autodesk Inventor/Vault Professional 2021
0 Likes
Message 9 of 14

Owner2229
Advisor
Advisor

Can you try different assembly and sheetmetal part?

I have tested it here and it works for me, so I have no idea why it doesn't for you.

 

Try to copy the rule from the attached text file.

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
Message 10 of 14

Anonymous
Not applicable

How can I delete a few specific pieces of flat pattern?

0 Likes
Message 11 of 14

Anonymous
Not applicable

Send me the code if you can

0 Likes
Message 12 of 14

Anonymous
Not applicable

Send me the code if you can

0 Likes
Message 13 of 14

Anonymous
Not applicable

Send me the code if you can ,merci

0 Likes
Message 14 of 14

A.Acheson
Mentor
Mentor

 

This post is old so it would be best to reference this post and also display the code you are using. 
You can filter the referenced document using the the  display name. 

 For Each oDoc As Document In xDoc.AllReferencedDocuments
If oDoc.DisplayName = “Insert part name here” Then
‘……..Insert rest of code here
End If
Next

The display name is the browser name of the part in the assembly This example shows it use. 

https://forums.autodesk.com/t5/inventor-ilogic-api-vba-forum/ilogic-display-name/td-p/9135009

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes