Run External Rule From an Assembly that affects the opened document

Run External Rule From an Assembly that affects the opened document

fabiorcferreira
Contributor Contributor
2,244 Views
14 Replies
Message 1 of 15

Run External Rule From an Assembly that affects the opened document

fabiorcferreira
Contributor
Contributor

I want to do the following:
Select a component in an assembly, open it, and execute an external rule that affect the opened part.
So far i have only accomplished to do this if instead of a external rule i add it to the part. This way the rule affects the part and not the assembly.


The problem is that whenever i try to use it with an external rule, it uses the data from the assembly.

 

I select a part (sheet metal) in an assembly and then i execute a macro that runs "Open_File"


Rule "Open_File":

 

'get currently selected component
Dim oOccurrence As ComponentOccurrence
Try
  oOccurrence = ThisDoc.Document.SelectSet.Item(1)
Catch
  MessageBox.Show("Please select a component before running this rule.", "iLogic")
  Return
End Try

Dim doc As Document
Dim CurFileName As String

'set the selected item
oOccurrence = ThisApplication.ActiveDocument.SelectSet.Item(1)

'get the selected item document occurrence name
doc = oOccurrence.Definition.Document

'Make file invisible
oOccurrence.Visible = False

Dim oName As String
    oName = oOccurrence.Name
	
'Open the file
ThisApplication.Documents.Open(doc.FullFileName ,True)
'doc.FileName(False) = Filenamed


'Run rule
'iLogicVb.RunRule("componentName", "ruleName")
'iLogicVb.RunRule(oName, "Export_DWG")
iLogicVb.RunExternalRule("C:\Users\fabio\Dropbox\INDUSSTOCK\Documentos CAD\_inventor definições_\Rules\Export_DWG")

 

Rule "Export_DWG":

 

'Check that this active document is a part file
	Imports System.Windows.Forms
	'Wait for file to open
		System.Threading.Thread.CurrentThread.Sleep(1000)
	If ThisApplication.ActiveDocument.DocumentType <> kPartDocumentObject Then
		MessageBox.Show ("Please open a part document", "iLogic")
	End If
	
'Unselect faces
	ThisApplication.ActiveDocument.Save

'Set a reference to the active 
	Dim oDoc As PartDocument
		oDoc = ThisApplication.ActiveDocument
	Dim oSelectSet As Selectset
		oSelectSet = oDoc.SelectSet
			oDoc.SelectSet.Clear()
	oQty = InputBox("Quantidade", "Quantidade", "1")
	'If cancel then stop
			If oQty = "" Then
				Return
			End If
	'Folder Name
		oFolder = ThisDoc.Path & "\Laser " & DateTime.Now.ToString("dd") & "_" & DateTime.Now.ToString("MM") & "\"
	'File Name
'doc.ComponentDefinition.Parameters.UserParameters	
		oFileName = ThisApplication.ActiveDocument.ComponentDefinition.Parameters.Parameter("Thickness")& "mm " & oQty & "x" & " "& ThisDoc.FileName(False) & ".dwg" 'without extension

'flat patternizor
Dim oCompDef As SheetMetalComponentDefinition
    oCompDef = oDoc.ComponentDefinition
	'Check for FlatPattern, if false create it, if true, unfold it
		If oCompDef.HasFlatPattern = False Then
			oCompDef.Unfold
		Else
			oCompDef.FlatPattern.Edit
		End If
		
'Check for the DXF folder and create it if it does not exist
	If Not System.IO.Directory.Exists(oFolder) Then
		System.IO.Directory.CreateDirectory(oFolder)
	End If

'SELECTION CODE, stop if operation is canceled (esc pressed)
	Dim oFace As Face 
	oFace = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFaceFilter, "Select a face")  
	If (oFace Is Nothing) Then
		Return
	Else
		Call oDoc.SelectSet.Select(oFace)
	End If
	
'Add filename to memory for file save dialog (i don't fully understand this :p )
	Dim Cm As CommandManager
		Cm = ThisApplication.CommandManager
		Cm.PostPrivateEvent(PrivateEventTypeEnum.kFileNameEvent, oFolder & oFilename )
		
'EXPORT CODE 1
	Dim oCtrlDef As ButtonDefinition
	oCtrlDef = ThisApplication.CommandManager.ControlDefinitions.Item("GeomToDXFCommand")
	Call oCtrlDef.Execute

'CLEAR selectset
	oDoc.SelectSet.Clear()
	
'save and close document
	ThisDoc.Save
	odoc.Close(True)

It seems that "Export_DWG" is affecting the assembly, as it is saying that the assembly file doesn't have the parameter "thickness"

0 Likes
Accepted solutions (2)
2,245 Views
14 Replies
Replies (14)
Message 2 of 15

MechMachineMan
Advisor
Advisor

Be very careful with your use of 

ThisApplication.ActiveDocument

as it gets a reference to the frontmost document open, instead of that which the rule was ran from. When a rule is run in the document, it DOES NOT become the frontmost document, and therefore is not the one picked up by the reference.

 

Instead, in your code use

 

ThisDoc.Document

as that will grab the document in which the rule is ran from, which might not necessarily be the frontmost document.


--------------------------------------
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
0 Likes
Message 3 of 15

fabiorcferreira
Contributor
Contributor

But the rule isn't run from the document i want it to affect, the rule gets run by another rule that's run from the assembly level. The rule that gets rune from the assembly level is supposed to open the part i want, and then rune an external rule that affects the document just opened, without any user input.
It seems that the rule is still affecting the assembly, as it says that parameter thickness was not found in the assembly.

0 Likes
Message 4 of 15

b_sharanraj
Advocate
Advocate

I can see that there is a checkpoint in External Rule for ActiveDocument is PartDocument or not are you getting a pop message of this condition?

 

	If ThisApplication.ActiveDocument.DocumentType <> kPartDocumentObject Then
		MessageBox.Show ("Please open a part document", "iLogic")
	End If

 

Regards

B.Sharan Raj

0 Likes
Message 5 of 15

fabiorcferreira
Contributor
Contributor

It doesn't, and that makes me very confused. Have a look at what happens.

 

https://i.gyazo.com/20e98a960e35a5e30f997fc89cd4d1d2.mp4

 

 

635dfd179e1e04d928c219011d4fec22

 

 

 

 

0 Likes
Message 6 of 15

b_sharanraj
Advocate
Advocate

I can see that there is no parameter with name "Thickness" has been called in Rule "Export_DWG"

 

So that some other rule might be trigger after changing the parameter "Quantidade" via Input Box.

 

Hence check for Driving Rule in Parameter Table which drives after changing the "Quantidade" Parameter

 

Regards

B.Sharan Raj

0 Likes
Message 7 of 15

fabiorcferreira
Contributor
Contributor

Yes there is, but it is shown like this:

 

'File Name
'doc.ComponentDefinition.Parameters.UserParameters	
		oFileName = ThisApplication.ActiveDocument.ComponentDefinition.Parameters.Parameter("Thickness")& "mm " & oQty & "x" & " "& ThisDoc.FileName(False) & ".dwg" 'without extension

 

but that was from a test rule, i'm using this, both give the same error thought:

 

'File Name	
	oFileName = Parameter("Thickness")& "mm " & oQty & "x" & " "& ThisDoc.FileName(False) & ".dwg" 'without extension
0 Likes
Message 8 of 15

b_sharanraj
Advocate
Advocate

Sorry i think i made spell check while using find command

 

This code will work perfectly no issue in code

 

oFileName = Parameter("Thickness")& "mm " & oQty & "x" & " "& ThisDoc.FileName(False) & ".dwg"

 

I can see that still the code was running in assembly not in part see the below screenshot from the video you uploaded

 

As @MechMachineMan said earlier still the code is running in your assembly not in part

 

image.png

Regards

B.Sharan Raj

0 Likes
Message 9 of 15

fabiorcferreira
Contributor
Contributor

Yes, i do know it is running from the assembly, but that is my problem, how can i make it run in the part?

0 Likes
Message 10 of 15

b_sharanraj
Advocate
Advocate
Accepted solution

Try this please

 

 

Dim Temp_Thick As String
Temp_Thick = ThisApplication.ActiveDocument.ComponentDefinition.Parameters.item("Thickness").Expression
oFileName = oQty & "x" & " "& ThisDoc.FileName(False) & ".dwg" 'without extension
MsgBox(Temp_Thick & oFileName)

 

Regards

B.Sharan Raj

Message 11 of 15

fabiorcferreira
Contributor
Contributor

It does output the right thickness, but the name still comes from the assembly.

What more should i change?

594620037fc892bff3869f6d3d318d4b

 

 

0 Likes
Message 12 of 15

b_sharanraj
Advocate
Advocate

I just added a line of msgbox to check correct file name occurs.

 

You can remove the below line from your code

 

MsgBox(Temp_Thick & oFileName)

 

Regards

B.Sharan Raj

0 Likes
Message 13 of 15

fabiorcferreira
Contributor
Contributor

That's not what i mean, i do need the filename to come from the part, not the assembly. That line is good for testing for now, but i need to output the part file name ("base1 IS") instead of the assembly that is currently outputting 

0 Likes
Message 14 of 15

b_sharanraj
Advocate
Advocate
Accepted solution

By executing the below code you can get the filename of Active Document 🙂

 

FullFileName = ThisApplication.ActiveDocument.FullFileName

FileName = FullFileName.Remove(0, FullFileName.LastIndexOf("\") + 1)

MsgBox(FileName)

 

Regards

B.Sharan Raj

0 Likes
Message 15 of 15

fabiorcferreira
Contributor
Contributor

Thanks, it is all working now thanks to you, based on the information that you gave me i was able to fix the other problems 

0 Likes