Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

ThisDoc.Document: Which document really?

7 REPLIES 7
Reply
Message 1 of 8
eljoseppo
6257 Views, 7 Replies

ThisDoc.Document: Which document really?


Hi,

I'm writing a rule which concerns a ipt file but I want to run it from iam level while I'm editing a ipt file. From the ipt level it should work as well but this is not a problem.

I have external rule and I get different answer form this code depending how I run it (* hope you understand my english 😉 )

Dim oDrawingDoc As DrawingDocument    
Dim oPartDoc As Document
Dim oBLACHA As Document

Dim oSheet As sheet
Dim oTitleBlock As TitleBlock
Dim oTextBoxes As TextBoxes
doc = ThisDoc.Document 'active?

'checking the file type
Select Case doc.DocumentType
Case 12290
part_type="IPT"
Case 12291
part_type="IAM"
Case 12292
part_type="IDW"
End Select

If part_type="IAM" Then
  MessageBox.Show("IAM", "File type")
  Exit Sub

If part_type="IPT" Then
 If doc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then
     MessageBox.Show("IPT Sheetmetal part", "File type")

'Goto SHEET_export
 Else 
  MessageBox.Show("It's IPT but not a sheetmetal part", "File type")
  Exit Sub
 End If

End If

If part_type="IDW" Then 
'  Goto IDW_export
End If 

 If I manually run this rule it works just fine but I want to use button, I bet u know this code:

Public Sub RuniLogic(ByVal RuleName As String)
  Dim iLogicAuto As Object
  Dim oDoc As Document

  Set oDoc = ThisApplication.ActiveDocument
  If oDoc Is Nothing Then
    MsgBox "Missing Inventor Document"
    Exit Sub
  End If

  Set iLogicAuto = GetiLogicAddin(ThisApplication)
  If (iLogicAuto Is Nothing) Then Exit Sub

  iLogicAuto.RunRule oDoc, RuleName
End Sub

Function GetiLogicAddin(oApplication As Inventor.Application) As Object
Set addIns = oApplication.ApplicationAddIns

'Find the add-in you are looking for
Dim addIn As ApplicationAddIn
On Error GoTo NotFound
Set addIn = oApplication.ApplicationAddIns.ItemById("{3bdd8d79-2179-4b11-8a5a-257b1c0263ac}")

If (addIn Is Nothing) Then Exit Function

addIn.Activate
Set GetiLogicAddin = addIn.Automation
Exit Function
NotFound:
End Function

Public Sub RuniExtLogic(ByVal RuleName As String)
  Dim iLogicAuto As Object
  Dim oDoc As Document

  Set oDoc = ThisApplication.ActiveDocument
  If oDoc Is Nothing Then
    MsgBox "Missing Inventor Document"
    Exit Sub
  End If

  Set iLogicAuto = GetiLogicAddin(ThisApplication)
  If (iLogicAuto Is Nothing) Then Exit Sub

On Error GoTo errorline

  iLogicAuto.RunExternalRule oDoc, RuleName
  Exit Sub
 
errorline:
    MsgBox "Missing External rule: " & RuleName
 
End Sub

For me it looks like the VBA code is setting the iam file as ThisApplication.ActiveDocument instead of my edited ipt file.

 

What is the syntax to change this code to ThisDoc.Document?

 

I'm a copy/paste type of programmer please forgive me if I make a basic mistakes.

 

What I intend to do is export a sheetmetal part with the drawing. No matter from which level (ipt iam idw) it will be run it should do exactly the same: export sheetmetal as a DXF, find a idw file (the same folder and file name, standard) export it as DWG. From the IAM lvl when I activate (edit) ipt file it should do the same. If it will be run from IDW just refer to the file on the drawing, easy thing.

I have 3 rules and I want to connect them somehow..

7 REPLIES 7
Message 2 of 8
xiaodong_liang
in reply to: eljoseppo

Hi,

 

I am answering what I understood with your questions:

 

1. you can simply add the VBA macro to Ribbon by right click the blank  area of Ribbon>>click [Custimize User Commands] and add the macro. See the attached fig.

 

2.  ThisDoc.Document is equivelent to the VBA code below:

 

      Dim oDoc As Document

      Set oDoc = ThisApplication.ActiveDocument

 

3.  about exporting DWF, perhaps the other thread may help:

  http://forums.autodesk.com/t5/Autodesk-Inventor-Customization/automation-using-ilogic/m-p/2762292/hi...

    

  

 

  

Message 3 of 8
eljoseppo
in reply to: xiaodong_liang

Thanks for the replay, It wont be easy

 

1 well, yes. But I wrote my rule in iLogic and when I'm running from VBA macro it starts to behave different.

 

2 As far as I noticed there is a difference. This document is this which contains the rule, ActiveDocument is the one is open whight now, and not always this is the same

 

3 thanks,

 

Please copy this in my first code window in first post and save it as iLogic rule, external one if you please.

 

Then try to run in in dirrefent files, idw ipt iam, and most important: from iam while you're editing ipt file (see attached fig). The rule should work fine.

 

Then try to compare if the macro will call out this rule. This macro is in my second code window in first post.

 

It works different if I have ipt edited in iam. I don't understand, why? I don't know how to make it behave like in the first time.

 

Thanks for your interest.

Message 4 of 8
NL-Laurens
in reply to: xiaodong_liang

In reply to Xianodong Liang answer 2:

 

I believe that ThisDoc.Document is slightly different then ThisApplication.ActiveDocument (iLogic enviroment)

 

I think ThisDoc always refers to the document that contains this iLogic rule, while the ActiveDocument not always is the document that contains the iLogic.

 

My sittuation:

 

I have an iLogic to sort the BOM in an assembly (triggered before save), when I change something in that assembly, don't save it, then go to the drawing that includes this assembly, there I save the drawing. It tells me that the Assembly also needs to be saved. At that save moment the iLogic in the assembly get's fired, but the activedocument is the drawing, this results in a crash of the iLogic. If you use ThisDoc.Document instead of ThisApplication.ActiveDocument then the iLogic doesn't crash because it is refering to the assembly and not the drawing.

 

Correct me if I'm wrong

 

Laurens

Message 5 of 8
eljoseppo
in reply to: NL-Laurens

You are right. That is my case. And I wanted to get some guidelines how to use it.

Simple replacement won't solve the issue.
Message 6 of 8
DeerSpotter
in reply to: eljoseppo

This code is amazing. Very Useful. Thank you!

You missed a "End If"

If part_type="IAM" Then
  MessageBox.Show("IAM", "File type")
  Exit Sub
End If 'missing End If added

 

Image and video hosting by TinyPic
..........................................................................................................................
Did you find this reply helpful ? If so please use the Accept as Solution or Kudos button below.
..........................................................................................................................


See My LinkedIn Profile
Message 7 of 8
philip1009
in reply to: eljoseppo

ThisApplication.ActiveDocument is for the document that is active in your Inventor Application when it's called in the rule.  If you have an assembly open and you're editing a sub-assembly or part from the assembly file, calling ThisApplication.ActiveDocument will get the top assembly instead of the part/assembly you're editing.

 

ThisDoc.Document is useful if you want to run a rule that's in either a sub-assembly or part or in another document that's open.  Calling ThisDoc.Document will reference the Document that the iLogic rule is running from instead of the active document.

 

If you want to reference the Document that you are editing from a higher assembly document, then you want to use ThisApplication.ActiveEditDocument.

 

Hopefully this makes enough sense for you.

Message 8 of 8
dustinr91
in reply to: philip1009

Thanks!! I was having a similar issue with running rules in sub-parts and this is a life saver!!!

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report