Run Rule Only When File Containing Rule Is The Active Document

Run Rule Only When File Containing Rule Is The Active Document

kwalker1
Enthusiast Enthusiast
1,252 Views
4 Replies
Message 1 of 5

Run Rule Only When File Containing Rule Is The Active Document

kwalker1
Enthusiast
Enthusiast

I have an iLogic rule in an assembly file which shrinkwraps the model and is triggered by the Close Document event.

Problem is the message box also appears when the drawing sheet containing views of this model is closed.

 

Can an iLogic be written to only run the rule when the active document in the graphics is the same document that contains the rule?

 

I have been able to write an iLogic which runs the rule only when the active document is an assembly file but I would like to change this to be the specific file that contains the rule.

 

My current rule is as follows:

 

Sub Main()
Try
Dim oDoc As Document = ThisApplication.ActiveDocument
If oDoc.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
Exit Sub
Else

Question = MessageBox.Show("Do you wish to shrinkwrap the Fan Drive Assembly now for use in the General Arrangement?", "Fan Drive Assembly",MessageBoxButtons.YesNo)
If Question = vbYes Then

Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition
oAsmCompDef.RepresentationsManager.DesignViewRepresentations.Item("GA - FAN ASSEMBLY VIEW").Activate

Try
    Dim g_App As Inventor.InventorServer = ThisApplication
    Dim AssDoc as Inventor.AssemblyDocument= ThisDoc.Document

    ' Create a new part document that will be the shrinkwrap substitute
    Dim oPartDoc As PartDocument
    oPartDoc = g_App.Documents.Add(DocumentTypeEnum.kPartDocumentObject, , True)

    Dim oPartDef As PartComponentDefinition
    oPartDef = oPartDoc.ComponentDefinition

    Dim oDerivedAssemblyDef As DerivedAssemblyDefinition
    oDerivedAssemblyDef = oPartDef.ReferenceComponents.DerivedAssemblyComponents.CreateDefinition(AssDoc.FullDocumentName)
    ' Set various shrinkwrap related options
    oDerivedAssemblyDef.DeriveStyle = DerivedComponentStyleEnum.kDeriveAsMultipleBodies
    oDerivedAssemblyDef.IncludeAllTopLevelWorkFeatures = DerivedComponentOptionEnum.kDerivedExcludeAll
    oDerivedAssemblyDef.IncludeAllTopLevelSketches = DerivedComponentOptionEnum.kDerivedExcludeAll
    oDerivedAssemblyDef.IncludeAllTopLeveliMateDefinitions = DerivedComponentOptionEnum.kDerivedExcludeAll
    oDerivedAssemblyDef.IncludeAllTopLevelParameters = DerivedComponentOptionEnum.kDerivedExcludeAll
    Call oDerivedAssemblyDef.SetHolePatchingOptions(DerivedHolePatchEnum.kDerivedPatchNone)
    Call oDerivedAssemblyDef.SetRemoveByVisibilityOptions(DerivedGeometryRemovalEnum.kDerivedRemoveNone)
    Dim oDerivedAss As DerivedAssemblyComponent
    oDerivedAss = oPartDoc.ComponentDefinition.ReferenceComponents.DerivedAssemblyComponents.Add(oDerivedAssemblyDef)
    Call oDerivedAss.BreakLinkToFile()

    ' Save the part
    Dim partname As String=ThisDoc.PathAndFileName(False)& " (SHRINKWRAPPED).ipt"
    ThisApplication.ActiveView.Fit
    ThisApplication.CommandManager.ControlDefinitions.Item("AppIsometricViewCmd").Execute
    Call oPartDoc.SaveAs(partname ,  False)
Catch ex As Exception
        ErrorMessage = "Error creating ipt file."
End Try

oAsmCompDef.RepresentationsManager.DesignViewRepresentations.Item("Default").Activate

Else
'Do Nothing
End If

End If
Catch
End Try
End Sub
0 Likes
Accepted solutions (1)
1,253 Views
4 Replies
Replies (4)
Message 2 of 5

chandra.shekar.g
Autodesk Support
Autodesk Support

 

Hi @kwalker1,

 

Please try the following iLogic rule. In this, iLogic rule should be mentioned while implementing(Highlighted in red color).

 

Sub Main()
Try
Dim oDoc As Document = ThisApplication.ActiveDocument

Dim iLogicAuto As Object
iLogicAuto = GetiLogicAddin(ThisApplication)

If iLogicAuto Is Nothing Then
	Exit Sub
End If

Dim rule As Object
rule = iLogicAuto.GetRule(doc, "Rule Name")

If oDoc.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Or  rule Is Nothing Then
Exit Sub
Else

Question = MessageBox.Show("Do you wish to shrinkwrap the Fan Drive Assembly now for use in the General Arrangement?", "Fan Drive Assembly",MessageBoxButtons.YesNo)
If Question = vbYes Then

Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition
oAsmCompDef.RepresentationsManager.DesignViewRepresentations.Item("GA - FAN ASSEMBLY VIEW").Activate

Try
    Dim g_App As Inventor.InventorServer = ThisApplication
    Dim AssDoc as Inventor.AssemblyDocument= ThisDoc.Document

    ' Create a new part document that will be the shrinkwrap substitute
    Dim oPartDoc As PartDocument
    oPartDoc = g_App.Documents.Add(DocumentTypeEnum.kPartDocumentObject, , True)

    Dim oPartDef As PartComponentDefinition
    oPartDef = oPartDoc.ComponentDefinition

    Dim oDerivedAssemblyDef As DerivedAssemblyDefinition
    oDerivedAssemblyDef = oPartDef.ReferenceComponents.DerivedAssemblyComponents.CreateDefinition(AssDoc.FullDocumentName)
    ' Set various shrinkwrap related options
    oDerivedAssemblyDef.DeriveStyle = DerivedComponentStyleEnum.kDeriveAsMultipleBodies
    oDerivedAssemblyDef.IncludeAllTopLevelWorkFeatures = DerivedComponentOptionEnum.kDerivedExcludeAll
    oDerivedAssemblyDef.IncludeAllTopLevelSketches = DerivedComponentOptionEnum.kDerivedExcludeAll
    oDerivedAssemblyDef.IncludeAllTopLeveliMateDefinitions = DerivedComponentOptionEnum.kDerivedExcludeAll
    oDerivedAssemblyDef.IncludeAllTopLevelParameters = DerivedComponentOptionEnum.kDerivedExcludeAll
    Call oDerivedAssemblyDef.SetHolePatchingOptions(DerivedHolePatchEnum.kDerivedPatchNone)
    Call oDerivedAssemblyDef.SetRemoveByVisibilityOptions(DerivedGeometryRemovalEnum.kDerivedRemoveNone)
    Dim oDerivedAss As DerivedAssemblyComponent
    oDerivedAss = oPartDoc.ComponentDefinition.ReferenceComponents.DerivedAssemblyComponents.Add(oDerivedAssemblyDef)
    Call oDerivedAss.BreakLinkToFile()

    ' Save the part
    Dim partname As String=ThisDoc.PathAndFileName(False)& " (SHRINKWRAPPED).ipt"
    ThisApplication.ActiveView.Fit
    ThisApplication.CommandManager.ControlDefinitions.Item("AppIsometricViewCmd").Execute
    Call oPartDoc.SaveAs(partname ,  False)
Catch ex As Exception
        ErrorMessage = "Error creating ipt file."
End Try

oAsmCompDef.RepresentationsManager.DesignViewRepresentations.Item("Default").Activate

Else
'Do Nothing
End If

End If
Catch
End Try
End Sub

Please feel free to contact if there is any doubt.

 

If solve problem, click on "Accept as solution" / give a "Kudo".

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 3 of 5

MechMachineMan
Advisor
Advisor
Accepted solution

If you are running this as a rule, in the iLogic environment, changing this:

 

Dim oDoc As Document = ThisApplication.ActiveDocument
If oDoc.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
Exit Sub
Else

to this:

 

Dim oDoc As Document = ThisApplication.ActiveDocument
If Not oDoc Is ThisDoc.Document Then Exit Sub

should work just fine.


--------------------------------------
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 4 of 5

kwalker1
Enthusiast
Enthusiast

Hi Chandra,

 

Thank you for your reply.

 

I have tried the modified code that you posted but I get the following error message:

Error on Line 6 : 'GetiLogicAddin' is not declared. It may be inaccessible due to its protection level.

 

I have not personally come across code with the GetiLogicAddin so I tried searching for it in the API Help to better understand it. But I can seem to get any results when searching for it in the help dialogue box.

 

Regards,

Kurt.

 

0 Likes
Message 5 of 5

kwalker1
Enthusiast
Enthusiast

Hi Justin,

 

Thank you very much for your reply.

 

I thought I was getting close with different variations of my code but just couldn't seem to get it right.

I've tried your suggestion and it is working perfectly.

My sincere thanks for your assistance.

 

Regards,

Kurt.

0 Likes