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

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

j.wolbers-NoTech
Enthusiast Enthusiast
2,454 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,455 Views
24 Replies
Replies (24)
Message 21 of 25

WCrihfield
Mentor
Mentor

I don't know if this will work for your situation or not, but within the Event Triggers dialog box, if you switch to the Parts tab, you could place your external iLogic rule under the "After Open Document" and/or "Before Save Document".  This will work for every Part document, not just the active part document. And if you have those safeguards in the rule that cause it to only work on SheetMetal parts, it shouldn't effect performance of other parts hardly at all.  This is often a good way to deal with all new and old files.

Also there may be a way to use the task scheduler to process a whole directory at a time.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 22 of 25

j.wolbers-NoTech
Enthusiast
Enthusiast

Hi @marcin_otręba 

 

I just tested your code.
The code itself works well!

 

However, the existing ilogic rule is not yet removed from the file (see image).

 

1.PNG

 

I also tried to replace the word '' bend '' with '' Bending '' in the piece of code below. This again shows that I don't understand much of Ilogic...

 

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 

 

 

Kind regards,
Jeffrey

0 Likes
Message 23 of 25

marcin_otręba
Advisor
Advisor
Accepted solution

Sorry,

 

i made mistake in line:

instead

If document.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then

wchich checks if document is sheetmetal document

i had

document.SubType ="{4D29B490-49B2-11D0-93C3-7E0706000000}"  - wchich is drawing document.

 

this one :

If Left(ruleName,4).ToLower="bend" Then

means that  if 4 chars from word rulename equals to "bend" then true

if you want to change it to word Bending  then go with:

If Left(ruleName,7).tolower="bending" Then

 

 

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 = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then
Auto = iLogicVb.Automation
Dim iLogicAuto As Object
iLogicAuto = Auto
Dim oDoc As document
oDoc = ThisApplication.ActiveDocument
MessageBox.Show("a")
'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
Message 24 of 25

j.wolbers-NoTech
Enthusiast
Enthusiast

@marcin_otręba, It works!!

 

I have only changed the following part from: MessageBox.Show ("a") To 'MessageBox.Show (" a ") so that the message box stays away. Otherwise, it will appear twice even if the Ilogic rule has already been removed.

 

That's not a problem, is it?

 

Thank you very much for your time and effort!

 

Kinds regards,
Jeffrey

0 Likes
Message 25 of 25

marcin_otręba
Advisor
Advisor
Accepted solution

sorry i forgot to delete it. you can delete it or comment it without problem.

Hi, maybe you want to check my apps:


DrawingTools   View&ColoringTools   MRUFolders

0 Likes