Ilogic code - Run external rule before save (sheetmetal only)

Ilogic code - Run external rule before save (sheetmetal only)

j.wolbers-NoTech
Enthusiast Enthusiast
2,446 Views
24 Replies
Message 1 of 25

Ilogic code - Run external rule before save (sheetmetal only)

j.wolbers-NoTech
Enthusiast
Enthusiast

Hi,

 

I use an ilogic code that recognizes when there is a bending in a sheet metal part. This code writes "Bend Exist" or "No Bend Exist" to a custom property in the part.

 

This code is included in the sheet metal template, which means that new files automatically receive this code. But unfortunately there are also parts that do not include this code. When such a part is copied, the code is missing again unless someone happens to think about it.

 

Now I would like to use this code as an External rule (and therefore also remove it from the template). However, I get the error below when trying to save a regular part (no sheet metal part). How to change the code so that it only runs on a sheet metal part.

 

1.PNG

 

Furthermore, I would like to see the "msgbox" close automatically after three seconds or not come up at all.

 

The ilogic code is stored in the system configuration folder within our Vault environment. So every user can access it. How do I ensure that this external ilogic rule works for all users '' automatically '' ...?

 

 

 

Public Sub Main()
Dim a As Inventor.Application
a = ThisApplication

Dim b As PartDocument
 b = ThisDoc.Document
 
Dim sm As SheetMetalComponentDefinition
sm = b.ComponentDefinition

MsgBox (sm.Bends.Count)
Dim customProp As Inventor.Property
On Error Resume Next
customProp = b.PropertySets.Item(4).Add("Bend Exist", "Bending")

If Err.Description Then
	Err.Clear
End If
customProp = b.PropertySets.Item(4).Item("Bending")

If sm.Bends.Count > 0 Then

customProp.Value = "Bend Exist"

Else If sm.Bends.Count = 0 Then	

customProp.Value = "No Bend Exist"

End If
End Sub

 

 

I hope someone can help me with this.

 

Kinds regards,

Jeffrey

0 Likes
Accepted solutions (2)
2,447 Views
24 Replies
Replies (24)
Message 2 of 25

ianteneth
Advocate
Advocate

Hi @j.wolbers-NoTech,

 

I found a previous post about determining if a part is a sheet metal part or not. Here is the code that checked that as well as the link to that post. Hope this helps!

 

This code will quit the program if it encounters a non-sheet metal part. You could adapt it to fit your needs.

 

' If part type equals normal type (non-sheet metal)
If oDoc.SubType = "{4D29B490-49B2-11D0-93C3-7E0706000000}"
	Exit Sub
End If

 

 

 

https://forums.autodesk.com/t5/inventor-customization/differentiate-sheet-metal-parts-in-ilogic/m-p/... 

0 Likes
Message 3 of 25

WCrihfield
Mentor
Mentor

There is also a readable way to check Document SubType by the documents iProperty.

 

Dim oDoc As Document = ThisApplication.ActiveDocument
Dim oPropSet As PropertySet = oDoc.PropertySets.Item("Design Tracking Properties")
Dim oProp As [Property] = oPropSet.Item("Document SubType Name")
MsgBox("Document SubType Name = " & oProp.Value)

If the active document is a Sheet Metal type Part document, and you run this codem, it should return "Sheet Metal".

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 4 of 25

j.wolbers-NoTech
Enthusiast
Enthusiast

Hi @ianteneth,

 

Thanks for your reply.

 

I tried to implement the code but I get below error. Any idea what I'm doing wrong? Unfortunately I don't know much about ilogic.

 

And is it possible to ensure that this code is used automatically by all users?

 

1.PNG2.PNG

 

 

 

Public Sub Main()
Dim a As Inventor.Application
a = ThisApplication

Dim b As PartDocument
 b = ThisDoc.Document

' If part type equals normal type (non-sheet metal)
If oDoc.SubType = "{4D29B490-49B2-11D0-93C3-7E0706000000}"
	End If
 
Dim sm As SheetMetalComponentDefinition
sm = b.ComponentDefinition

MsgBox (sm.Bends.Count)
Dim customProp As Inventor.Property
On Error Resume Next
customProp = b.PropertySets.Item(4).Add("Bend Exist", "Bending")

If Err.Description Then
	Err.Clear
End If
customProp = b.PropertySets.Item(4).Item("Bending")

If sm.Bends.Count > 0 Then

customProp.Value = "Bend Exist"

Else If sm.Bends.Count = 0 Then	

customProp.Value = "No Bend Exist"

End If
End Sub 

 

 

 

And is it possible that I want to automatically remove the existing Ilogic code from copied files that already contain an Ilogic code?

This could perhaps be done by looking for an Ilogic rule with the name "Bending" or "Bend" in the file.

 

Regards,

Jeffrey

0 Likes
Message 5 of 25

ianteneth
Advocate
Advocate

There were quite a few problems with the code you posted. Below is the rewritten code that should do what you want. It will put the number of bends in the sheet metal part in a custom iProperty named "Bending". I checked it and it seemed to work for me.

 

Public Sub Main()
	' Get the current document
	Dim document As Document = ThisApplication.ActiveDocument
	
	' Check if document is a part
	If document.DocumentType = kPartDocumentObject Then
		
		' Check if part is a sheet metal part
		If document.SubType <> "{4D29B490-49B2-11D0-93C3-7E0706000000}" Then
			Dim sheetMetal As SheetMetalComponentDefinition = document.ComponentDefinition
			
			' Get number of bends
			Dim bendCount As Integer = sheetMetal.Bends.Count	
			' Display bend count
			MessageBox.Show(bendCount)
			' Custom iproperty.
			Dim bendCountIProperty As Inventor.Property
			
			' Try to create iProperty. But it may exist already.
			Try 
				' Try to create iProperty.
				bendCountIProperty = document.PropertySets.Item(4).Add("", "Bending")
			Catch
				' Otherwise get the existing iProperty.
				bendCountIProperty = document.PropertySets.Item(4).Item("Bending")
			End Try
			
			' Check if there are bends or not. Set custom iProperty value.
			If bendCount > 0 Then
				bendCountIProperty.Value = "Bend Exist"
			Else 
				bendCountIProperty.Value = "No Bend Exist"
			End If
		End If		
		
	End If
End Sub 

 

0 Likes
Message 6 of 25

ianteneth
Advocate
Advocate

And as for your other concerns:

  • Making sure this code is used automatically by every user and every sheet metal part: I have this problem at my work. Users copy old parts or don't use the templates that have the iLogic code in them. I have not found a solution to this other than better training. Maybe someone else can advise us further on this.

 

 

0 Likes
Message 7 of 25

WCrihfield
Mentor
Mentor

There are ways to inject local rules into other documents from outside those documents.  There is a tool called Code-Injector, that may be able to help with this.  I'm not sure, but it may not be working with the latest version of Inventor at the moment, though.

I also know how to create and place a local rule into a document, using an external rule.  If that sounds like something that would work for you, I can probably help you out with that.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 8 of 25

Darkforce_the_ilogic_guy
Advisor
Advisor

just use this 

 

Sub Main()
	Dim doc = ThisDoc.Document
	Dim sDocumentSubType As String = doc.SubType
	Dim sm As SheetMetalComponentDefinition

Dim b As PartDocument
 b = ThisDoc.Document
	
'Undersøger Document type"
	Try
	If sDocumentSubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then	' = "Sheet Metal"
		

		
			
			


 

sm = b.ComponentDefinition





If sm.Bends.Count > 0 Then
iProperties.Value("Custom", "Bending")  = "Bend Exist"


Else If sm.Bends.Count = 0 Then	
iProperties.Value("Custom", "Bending")  = "No Bend Exist"

End If



			
		


		
	End If
	
Catch 
	
End Try
	
	End Sub

 

hope this is what you need

0 Likes
Message 9 of 25

you may need to copy 

 

iProperties.Value("Custom", "Bending")  = "No Bend Exist"

and indset it after Catch...

 

the code make an error if you save an Emtry doc   or you make want sort of popup  .. your choss of cause 

0 Likes
Message 10 of 25

j.wolbers-NoTech
Enthusiast
Enthusiast

Hi @ianteneth         [@WCrihfield]

 

Your code works perfectly @ianteneth .


Have only removed the line for the message box so that this pop-up is no longer visible.

 

Actually, I would like to process in this code as a first step that the ilogic rule with the name bending is removed if it exists. I have quite a few files where the code is processed in the part. And since parts are copied regularly, I would like to remove this specific code first and then run your code. It does not matter to me whether they are 2 separate external ilogic rules or one combined external rule.

Otherwise I run the ilogic rule double and I still get the pop-up.

 

I really appreciate the help.

 

Regards,

Jeffrey

0 Likes
Message 11 of 25

j.wolbers-NoTech
Enthusiast
Enthusiast

Nobody who can help with this?

 

 

 

0 Likes
Message 12 of 25

marcin_otręba
Advisor
Advisor

Hi,

did you try to search it at this forum in first place ?:

 

https://forums.autodesk.com/t5/inventor-forum/how-to-use-ilogic-rule-to-delete-rules-and-features-in...

 

Give me know if it is enough, and you are able to make it work?

Hi, maybe you want to check my apps:


DrawingTools   View&ColoringTools   MRUFolders

0 Likes
Message 13 of 25

JelteDeJong
Mentor
Mentor

try this rule

Dim oDoc As Document = ThisDoc.Document
If oDoc.DocumentType <> DocumentTypeEnum.kPartDocumentObject Then
    Return
End If
If oDoc.SubType <> "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then
    Return
End If

Dim sm As SheetMetalComponentDefinition = oDoc.ComponentDefinition
Dim customProp As Inventor.Property

Try
    customProp = oDoc.PropertySets.Item(4).Add("Bend Exist", "Bending")
Catch ex As Exception
    customProp = oDoc.PropertySets.Item(4).Item("Bending")
End Try

Try
    If sm.Bends.Count > 0 Then
        customProp.Value = "Bend Exist"
    Else
        customProp.Value = "No Bend Exist"
    End If
Catch ex As Exception
    MsgBox("Could not set iProperty 'Bending'. Problay there is none geometry jet")
End Try

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

0 Likes
Message 14 of 25

have you try my code ?

0 Likes
Message 15 of 25

sorry i did not read you need samething else too.

 

You are talking about you want it to be delete when you copy right ?

 

how are you making an copy ? are you copying in vault ? or save as /save copy as ? or how do you do it ?

 

if you are using vault you can use action rules in copy design... are you sure you need to delete it  or will an simple update the text/properties not do what you need ?

0 Likes
Message 16 of 25

j.wolbers-NoTech
Enthusiast
Enthusiast

@marcin_otręba 

I have checked this, but unfortunately I do not have enough programming knowledge to familiarize myself with this.

 

@Darkforce_the_ilogic_guy 

I use both methods for copying files.

 

 

@marcin_otręba @Darkforce_the_ilogic_guy 

I try to explain clearly below what the problem is and what I am trying to achieve.

 

At the moment I have a lot of files that contain an ilogic code that checks whether there is a bending in the part or not. This code is automatically added to new files since it is included in the sheet metal template.

 

Unfortunately, there are also files that are older and that do not yet contain this code. Logically, when I copy one of these files, the ilogic code is not included. It is almost impossible to check this manually every time and mistakes are quickly made. Sometimes the code is manually added in such a copied file, but then the '' Ilogic event trigger '' is unfortunately not always set. This means that there is a good chance that information in the custom property is no longer correct.

 

My idea was to run the ilogic code as an external rule and extract it from the template. As a result, the rule is always executed with sheet metal files.

 

However, the original Ilogic '' bending '' code must be removed from the copied files, otherwise the code will be run twice (1x in the file itself and 1x as an external rule).

 

I would also like the message box to no longer be shown, but that has already been resolved. This caused me problems while migrating the CAD data base. Every time this popup came up during the migration process and it doesn't make you happy with thousands of files.

 

The Ilogic code of @ianteneth  works perfectly. I am only looking for a '' small '' addition.

 

I hope I have described it a little more clearly.

 

Kinds regards,

Jeffrey

 

0 Likes
Message 17 of 25

marcin_otręba
Advisor
Advisor

Does your rule have always the same name?, or you want to delete all rules from file ?

 

Public Sub Main()
	' Get the current document
	Dim document As Document = ThisApplication.ActiveDocument
	
	' Check if document is a part
	If document.DocumentType = kPartDocumentObject Then
		
		' Check if part is a sheet metal part
		If document.SubType <> "{4D29B490-49B2-11D0-93C3-7E0706000000}" Then
			Dim sheetMetal As SheetMetalComponentDefinition = document.ComponentDefinition
			
			' Get number of bends
			Dim bendCount As Integer = sheetMetal.Bends.Count	
			' Display bend count
			MessageBox.Show(bendCount)
			' Custom iproperty.
			Dim bendCountIProperty As Inventor.Property
			
			' Try to create iProperty. But it may exist already.
			Try 
				' Try to create iProperty.
				bendCountIProperty = document.PropertySets.Item(4).Add("", "Bending")
			Catch
				' Otherwise get the existing iProperty.
				bendCountIProperty = document.PropertySets.Item(4).Item("Bending")
			End Try
			
			' Check if there are bends or not. Set custom iProperty value.
			If bendCount > 0 Then
				bendCountIProperty.Value = "Bend Exist"
			Else 
				bendCountIProperty.Value = "No Bend Exist"
			End If
		End If		
		If document.SubType = "{4D29B490-49B2-11D0-93C3-7E0706000000}" Then
		Auto = iLogicVb.Automation
Dim iLogicAuto As Object
iLogicAuto = Auto
Dim oDoc As document
oDoc = ThisApplication.ActiveDocument

'Master file name
If Not (ThisDoc.FileName(False)) = "iLogicPart" Then
	Dim ruleName As String
	Dim rules As Object
	rules = iLogicAuto.rules(oDoc)
		If Not (rules Is Nothing) Then
			For Each rule In rules
				ruleName = rule.Name
    			iLogicAuto.DeleteRule(oDoc, ruleName)
			Next 
		End If
End If
	End If
End If
End Sub 

Hi, maybe you want to check my apps:


DrawingTools   View&ColoringTools   MRUFolders

0 Likes
Message 18 of 25

marcin_otręba
Advisor
Advisor

or you can use

 

iLogicAuto.DeleteAllRulesInDocument(oDoc)

 

instead of

	Auto = iLogicVb.Automation
Dim iLogicAuto As Object
iLogicAuto = Auto
Dim oDoc As document
oDoc = ThisApplication.ActiveDocument

'Master file name
If Not (ThisDoc.FileName(False)) = "iLogicPart" Then
	Dim ruleName As String
	Dim rules As Object
	rules = iLogicAuto.rules(oDoc)
		If Not (rules Is Nothing) Then
			For Each rule In rules
				ruleName = rule.Name
    			iLogicAuto.DeleteRule(oDoc, ruleName)
			Next 
		End If
End If

 

Hi, maybe you want to check my apps:


DrawingTools   View&ColoringTools   MRUFolders

0 Likes
Message 19 of 25

j.wolbers-NoTech
Enthusiast
Enthusiast

Hi @marcin_otręba 

 

The Ilogic rule to be removed is called:

 

  • Bending (99% of the cases)
  • bending
  • Bend
  • bend

So if it is only possible to delete one Ilogic rule I would like the rule named "Bending" to be removed (because it is the most common).

 

I don't want all Ilogic rules to be removed as it may contain other rules as well.

 

When I read your Ilogic code I see that a message box is coming up.

I would like to prevent this.

Or am I not reading your Ilogic code correctly?

 

Regards,

Jeffrey

 

0 Likes
Message 20 of 25

marcin_otręba
Advisor
Advisor

This one should delete :

  • Bending (99% of the cases)
  • bending
  • Bend
  • bend

msgbox commented- will not show.

 

 

 

Public Sub Main()
	' Get the current document
	Dim document As Document = ThisApplication.ActiveDocument
	
	' Check if document is a part
	If document.DocumentType = kPartDocumentObject Then
		
		' Check if part is a sheet metal part
		If document.SubType <> "{4D29B490-49B2-11D0-93C3-7E0706000000}" Then
			Dim sheetMetal As SheetMetalComponentDefinition = document.ComponentDefinition
			
			' Get number of bends
			Dim bendCount As Integer = sheetMetal.Bends.Count	
			' Display bend count
			'MessageBox.Show(bendCount)
			' Custom iproperty.
			Dim bendCountIProperty As Inventor.Property
			
			' Try to create iProperty. But it may exist already.
			Try 
				' Try to create iProperty.
				bendCountIProperty = document.PropertySets.Item(4).Add("", "Bending")
			Catch
				' Otherwise get the existing iProperty.
				bendCountIProperty = document.PropertySets.Item(4).Item("Bending")
			End Try
			
			' Check if there are bends or not. Set custom iProperty value.
			If bendCount > 0 Then
				bendCountIProperty.Value = "Bend Exist"
			Else 
				bendCountIProperty.Value = "No Bend Exist"
			End If
		End If		
		If document.SubType = "{4D29B490-49B2-11D0-93C3-7E0706000000}" Then
		Auto = iLogicVb.Automation
Dim iLogicAuto As Object
iLogicAuto = Auto
Dim oDoc As document
oDoc = ThisApplication.ActiveDocument

'Master file name
If Not (ThisDoc.FileName(False)) = "iLogicPart" Then
	Dim ruleName As String
	Dim rules As Object
	rules = iLogicAuto.rules(oDoc)
		If Not (rules Is Nothing) Then
			For Each rule In rules
				ruleName = rule.Name
				If Left(ruleName,4).ToLower="bend" Then
    			iLogicAuto.DeleteRule(oDoc, ruleName)
			End If
			Next 
		End If
End If
	End If
End If
End Sub 

 

Hi, maybe you want to check my apps:


DrawingTools   View&ColoringTools   MRUFolders

0 Likes