Ilogic rule in part running in drawing

Ilogic rule in part running in drawing

Anonymous
Not applicable
2,270 Views
13 Replies
Message 1 of 14

Ilogic rule in part running in drawing

Anonymous
Not applicable

Eey,

 

I have this rule to set some parameters and drawingnumbers which runs before saving the part or assembly. This works fine. But when I change the part and open the drawing whitout saving the part first, and then try to save the drawing, inventor also needs to save the part. My rule will also run then, but since the drawing is the active document instead of the part or drawing my rule gets an error in setting the correct document class. Is there some code that solves this?

 

Thanks!

 

Kind regards,

 

Tommy

0 Likes
Accepted solutions (2)
2,271 Views
13 Replies
Replies (13)
Message 2 of 14

Jef_E
Collaborator
Collaborator

Hi,

 

It would be helpful to share the code that you have now, so we can review it and work from there.



Please kudo if this post was helpfull
Please accept as solution if your problem was solved

Inventor 2014 SP2
Message 3 of 14

Anonymous
Not applicable

This is the code I have so far

Sub Main


'Check if part is purchased or not
	Workpath= "I:\Engineering\"
	If Left(ThisDoc.Path,15)=WorkPath Then
	Exit Sub
	Else
	End If
	
	If iProperties.Value("Project", "Part Number") = "" Then
	Exit Sub
	Else
'Check for sheetmetal part
	If Right(ThisDoc.FileName(True),3)="iam" Then
	iProperties.Value("Custom","KFactor")= "Nvt"
	Exit Sub
	Else
		Dim oDoc As PartDocument
	oDoc = ThisApplication.ActiveDocument
	End If
	Dim oType As String
	oType = oDoc.DocumentSubType.DocumentSubTypeID
	
	If oType =  "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then
		iProperties.Value("Custom", "X_Maat") = SheetMetal.FlatExtentsLength
		iProperties.Value("Custom", "Y_Maat") = SheetMetal.FlatExtentsWidth
		Dim oSMDef As SheetMetalComponentDefinition
		oSMDef = oDoc.ComponentDefinition
		oSMDef.FlatPattern.ExitEdit
		iProperties.Value("Custom", "Part_Dimension")="Plaat " & Round(iProperties.Value("Custom", "X_Maat"),1) & "x" & Round(iProperties.Value("Custom", "Y_Maat"),1) & "x" & Parameter("Thickness")
		iProperties.Value("Custom","KFactor")=SheetMetal.ActiveKFactor	
	Else
	iProperties.Value("Custom","KFactor")= "Nvt"	
	End If
End If
iLogicVb.UpdateWhenDone = True
End Sub
0 Likes
Message 4 of 14

Jef_E
Collaborator
Collaborator

I tried to clean the code a little to make it more read-able and change some check for document type etc. Let me know if this works and then we can further check the error message you get from the drawing. 

 

If this code gives you an error please let me know what the error was ( screenshot of it or copy past.. )

 

        ' Get the active document object
        Dim oDoc As Document
        oDoc = ThisDoc


        ' Set the workpath
        Dim oWorkPath As String
        oWorkPath = "I:\Engineering\"

        ' Check if the document is in the workpath
        If oDoc.FullFileName.Contains(oWorkPath) Then

            ' Get the document properties
            Dim oPropertySet As PropertySet
            oPropertySet = oDoc.PropertySets.Item("Design Tracking Properties")

            ' Check the document part number
            If oPropertySet.Item("Part Number").Value = "" Then
                Exit Sub
            End If

            ' Check the document type
            If oDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
                oDoc.PropertySets.Item("User Defined Properties").Item("KFactor").Value = "Nvt"

            ElseIf oDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then

                ' Get the part document object
                Dim oPartDoc As PartDocument
                oPartDoc = CType(oDoc, PartDocument)

                ' Check if the sub document type is sheetmetal
                If oPartDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then

                    ' Get the sheetmetal component definition
                    Dim oDef As SheetMetalComponentDefinition
                    oDef = oPartDoc.ComponentDefinition

                    ' Set the property set to the custom iPoperties
                    oPropertySet = oPartDoc.PropertySets.Item("User Defined Properties")

                    ' Get the sheetmetal sizes
                    oPropertySet.Item("X_Maat").Value = oDef.FlatPattern.Length
                    oPropertySet.Item("X_Maat").Value = oDef.FlatPattern.Width
                    oPropertySet.Item("Part_Dimension").Value = "Plaat " & oDef.FlatPattern.Width & " x " & oDef.FlatPattern.Width & " x " & oDef.ActiveSheetMetalStyle.Thickness
                    oPropertySet.Item("KFactor").Value = oDef.ActiveSheetMetalStyle.UnfoldMethod.kFactor

                    ' Exit the flatpattern ( if required?)
                    oDef.FlatPattern.ExitEdit()

                Else
                    oDoc.PropertySets.Item("User Defined Properties").Item("KFactor").Value = "Nvt"
                End If
            End If
        End If


Please kudo if this post was helpfull
Please accept as solution if your problem was solved

Inventor 2014 SP2
Message 5 of 14

Anonymous
Not applicable

Thanks for cleaning up the code. I hae no idea what i'm doing:P Just copy pasting from the forums and get it working...

I tried the code but still got an error. I attached an screenshot of the error.

 

0 Likes
Message 6 of 14

Jef_E
Collaborator
Collaborator

And this error occurs when you press save in the drawing and the part is also saved. If you find it hard to communicate I can also assist in Dutch?



Please kudo if this post was helpfull
Please accept as solution if your problem was solved

Inventor 2014 SP2
Message 7 of 14

Anonymous
Not applicable

Hoi Jef,

 

Nederlands is geen probleem! Ja als ik het part wijzig en daarna vanuit de tekening wil opslaan dan krijg ik deze melding.

 

Gr,

 

Tommy

0 Likes
Message 8 of 14

Jef_E
Collaborator
Collaborator

Hi Tommy,

 

I made this small workaround ( I will respond in English so other users can also benefit from this conversation). I added a documents.open command so that the error will not persist.

 

Additional problem is that the document will close at the end, but this must not happen when you are in part environment so i added a test.

 

I hope this works, it's untested.

 

 ' Get the Inventor Application
        Dim oInvApp As Application
        oInvApp = ThisApplication

        ' Get the active document object
        Dim oDoc As Document
        oDoc = ThisDoc

        Dim oOpenCheck As Boolean
        oOpenCheck = oDoc.Open

        If Not oOpenCheck Then
            oDoc = oInvApp.Documents.Open(oDoc.FullFileName, False)
        End If

        ' Set the workpath
        Dim oWorkPath As String
        oWorkPath = "I:\Engineering\"

        ' Check if the document is in the workpath
        If oDoc.FullFileName.Contains(oWorkPath) Then

            ' Get the document properties
            Dim oPropertySet As PropertySet
            oPropertySet = oDoc.PropertySets.Item("Design Tracking Properties")

            ' Check the document part number
            If oPropertySet.Item("Part Number").Value = "" Then
                Exit Sub
            End If

            ' Check the document type
            If oDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
                oDoc.PropertySets.Item("User Defined Properties").Item("KFactor").Value = "Nvt"

            ElseIf oDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then

                ' Get the part document object
                Dim oPartDoc As PartDocument
                oPartDoc = CType(oDoc, PartDocument)

                ' Check if the sub document type is sheetmetal
                If oPartDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then

                    ' Get the sheetmetal component definition
                    Dim oDef As SheetMetalComponentDefinition
                    oDef = oPartDoc.ComponentDefinition

                    ' Set the property set to the custom iPoperties
                    oPropertySet = oPartDoc.PropertySets.Item("User Defined Properties")

                    ' Get the sheetmetal sizes
                    oPropertySet.Item("X_Maat").Value = oDef.FlatPattern.Length
                    oPropertySet.Item("X_Maat").Value = oDef.FlatPattern.Width
                    oPropertySet.Item("Part_Dimension").Value = "Plaat " & oDef.FlatPattern.Width & " x " & oDef.FlatPattern.Width & " x " & oDef.ActiveSheetMetalStyle.Thickness
                    oPropertySet.Item("KFactor").Value = oDef.ActiveSheetMetalStyle.UnfoldMethod.kFactor

                    ' Exit the flatpattern ( if required?)
                    oDef.FlatPattern.ExitEdit()

                Else
                    oDoc.PropertySets.Item("User Defined Properties").Item("KFactor").Value = "Nvt"
                End If
            End If
        End If

        ' Close the document
        If Not oOpenCheck Then
            oDoc.Close()
        End If

 



Please kudo if this post was helpfull
Please accept as solution if your problem was solved

Inventor 2014 SP2
Message 9 of 14

Anonymous
Not applicable

**** i clicked solved instead of reply...

 

Thanks for the update but I still get the same error. But now also in the part model.

0 Likes
Message 10 of 14

Jef_E
Collaborator
Collaborator

The error is in this line

 

oDoc = ThisDoc

I'm not so familiar with iLogic, and I tought I could obtain the object by using ThisDoc but apparently not, and I think if I use oInvApp.ActiveDocument the drawing is returned?

 

 



Please kudo if this post was helpfull
Please accept as solution if your problem was solved

Inventor 2014 SP2
Message 11 of 14

MechMachineMan
Advisor
Advisor

So close, @Jef_E

 

What you are missing is that a rule will always have issues pulling in that they most often just pull from the ActiveDocument.

 

The easiest way to deal with this is to check if there is an issue using programmatic error checking, and then handle the issue based upon that.

 

Please note, I haven't tested the rule below but there is a chance there will be some minor bugs that require fixing to get it working.

 

' Get the Inventor Application
        Dim oInvApp As Application
        oInvApp = ThisApplication

        ' Get the active document object
        Dim oDoc As Document
        oDoc = oInvApp.ActiveDocument

	If oDoc.DocumentType() = DocumentTypeEnum.kDrawingDocumentObject '12292
		Try
			oDoc = oDoc.Sheeets(1).DrawingViews(1).ReferencedDocumentDescriptor().ReferencedDocument()
		Catch
			MsgBox("RULE ABORTED!" & vblf & vblf & "Rule is being used in a drawing and there is issues pulling references from View 1 on Sheet 1!" & vblf & "Please manually run rule in part then try again!")
			Exit Sub
		End Try
	Else if oDoc.DocumentType() = DocumentTypeEnum.kAssemblyDocumentObject '12291
		If MsgBox("The rule will be run to modify an assembly file, if this is not your intent, exit now and rule the the rule in the part manually" & vblf & vblf & "Exit Now?", vbYesNo) = vbYes
			Exit Sub
		End if
	End if

        ' Set the workpath
        Dim oWorkPath As String
        oWorkPath = "I:\Engineering\"

        ' Check if the document is in the workpath
        If oDoc.FullFileName.Contains(oWorkPath) Then

            ' Get the document properties
            Dim oPropertySet As PropertySet
            oPropertySet = oDoc.PropertySets.Item("Design Tracking Properties")

            ' Check the document part number
            If oPropertySet.Item("Part Number").Value = "" Then
                Exit Sub
            End If

            ' Check the document type
            If oDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
                oDoc.PropertySets.Item("User Defined Properties").Item("KFactor").Value = "Nvt"

            ElseIf oDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then

                ' Get the part document object
                Dim oPartDoc As PartDocument
                oPartDoc = CType(oDoc, PartDocument)

                ' Check if the sub document type is sheetmetal
                If oPartDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then

                    ' Get the sheetmetal component definition
                    Dim oDef As SheetMetalComponentDefinition
                    oDef = oPartDoc.ComponentDefinition

                    ' Set the property set to the custom iPoperties
                    oPropertySet = oPartDoc.PropertySets.Item("User Defined Properties")

                    ' Get the sheetmetal sizes
                    oPropertySet.Item("X_Maat").Value = oDef.FlatPattern.Length
                    oPropertySet.Item("X_Maat").Value = oDef.FlatPattern.Width
                    oPropertySet.Item("Part_Dimension").Value = "Plaat " & oDef.FlatPattern.Width & " x " & oDef.FlatPattern.Width & " x " & oDef.ActiveSheetMetalStyle.Thickness
                    oPropertySet.Item("KFactor").Value = oDef.ActiveSheetMetalStyle.UnfoldMethod.kFactor

                    ' Exit the flatpattern ( if required?)
                    oDef.FlatPattern.ExitEdit()

                Else
                    oDoc.PropertySets.Item("User Defined Properties").Item("KFactor").Value = "Nvt"
                End If
            End If
        End If

        ' Close the document
        If Not oOpenCheck Then
            oDoc.Close()
        End If

--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
Message 12 of 14

Anonymous
Not applicable

Hey Justin,

Thanks for the reply!

I think this is a good workaround and works fine when saving a drawing. Only when saving an assembly or part, inventor crashes. Also, I don't want my part to close in the end but I can just turn that of in the rule.

 

UPDATE

Solved the crashing by deleting the part that closed the document

0 Likes
Message 13 of 14

Anonymous
Not applicable
Accepted solution

Ok I solved it a little different then i intended but I now use the following code when saving the part

Sub Main
		Dim oInvApp As Application
        oInvApp = ThisApplication
' Get the active document object
        Dim oDoc As Document
        oDoc = oInvApp.ActiveDocument

		If oDoc.DocumentType() = DocumentTypeEnum.kAssemblyDocumentObject Then
		iProperties.Value("Custom","KFactor")= "Nvt"
		Exit Sub
		End If
'Check if part is purchased or not
		Workpath= "I:\Engineering\"
		If Left(ThisDoc.Path,15)=WorkPath Then
		Exit Sub
		End If

'Check for sheetmetal part
		Dim oType As String
		oType = oDoc.DocumentSubType.DocumentSubTypeID
	
		If oType =  "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then
		iProperties.Value("Custom", "X_Maat") = SheetMetal.FlatExtentsLength
		iProperties.Value("Custom", "Y_Maat") = SheetMetal.FlatExtentsWidth
		Dim oSMDef As SheetMetalComponentDefinition
		oSMDef = oDoc.ComponentDefinition
		oSMDef.FlatPattern.ExitEdit
		iProperties.Value("Custom", "Part_Dimension")="Plaat " & Round(iProperties.Value("Custom", "X_Maat"),1) & "x" & Round(iProperties.Value("Custom", "Y_Maat"),1) & "x" & Parameter("Thickness")
		iProperties.Value("Custom","KFactor")=SheetMetal.ActiveKFactor	
		Else
		iProperties.Value("Custom","KFactor")= "Nvt"	
		End If
		iLogicVb.UpdateWhenDone = True
End Sub

Which does what i need when saving the part. When I'm saving the drawing i have another rule which runs before saving the drawing.

 

Sub Main()
If iProperties.Value("Project", "Part Number") = "" Then
Exit Sub
Else
curDoc = ThisDrawing.Sheet("sheet:1").View("VIEW1").ModelDocument
	
	If curDoc.DocumentType = kPartDocumentObject And curDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then
		oDoc = ThisApplication.Documents.Open(curDoc.FullFileName, True)
		oCompDef = oDoc.ComponentDefinition
		iLogicVb.RunExternalRule("Drawingnumber")
		ThisApplication.ActiveDocument.Close
	Else
	Return
	End If
End If
End Sub

This way the drawing always has the correct parameters. It takes a little more time to save, but thats ok by me.

Thanks for the help!

0 Likes
Message 14 of 14

Anonymous
Not applicable
Accepted solution

All you need to do is replace ActiveDocument in your iLogic rule in the part/assembly. If you add a custom property called Filename, you can use the following code to cause your rule to run without error: 

 

Dim oDoc As AssemblyDocument 'Or PartDocument

If iProperties.Value("Custom", "Filename")="" Then
    oDoc = ThisApplication.ActiveEditDocument
    iProperties.Value("Custom", "Filename")=oDoc.FullDocumentName
Else: oDoc= ThisApplication.Documents.ItemByName(iProperties.Value("Custom", "Filename"))
End If

As long as the Filename property is populated in your assembly or part, your rule will run even if the drawing is the active document.