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: 

How to use iLogic Rule to get the first file name in an assembly tree?

15 REPLIES 15
SOLVED
Reply
Message 1 of 16
zdhrichard
4458 Views, 15 Replies

How to use iLogic Rule to get the first file name in an assembly tree?

I need get information from the first file (it could be an *.ipt or *.iam). I want to run an iLogic Rule to get the name of the first file.

Could someone please help?

Thanks lot.

Rich

Autodesk Inventor Professional 2018 (64 Bit Edition)
Build: 284, Release 2018.3
Windows 7 Professional Service Pack 1
Intel(R) Xeon(R) CPU E5645
12.0 GB Memory
15 REPLIES 15
Message 2 of 16
cwhetten
in reply to: zdhrichard

How about this:

 

Dim oAsmComp As ComponentDefinition
oAsmComp = ThisDoc.Document.ComponentDefinition

Dim oFirstName As String
oFirstName = oAsmComp.Occurrences(1).ReferencedFileDescriptor.FullFileName

 

 

Please click "Accept as Solution" if this response answers your question.

Cameron Whetten
Inventor 2014

Message 3 of 16
zdhrichard
in reply to: cwhetten

Great! It is what I want.

Thanks lot.

Rich

Autodesk Inventor Professional 2018 (64 Bit Edition)
Build: 284, Release 2018.3
Windows 7 Professional Service Pack 1
Intel(R) Xeon(R) CPU E5645
12.0 GB Memory
Message 4 of 16
zdhrichard
in reply to: cwhetten

Hello Cameron,

When I use this rule, I find there is error when the assembly is empty. Could you please help me how to know the assembly is empty by iLogic rule?

I will use it as templete, so the assembly file name is unknow, and all occurrence names are unknow too.

Thanks lot.

Rich

Autodesk Inventor Professional 2018 (64 Bit Edition)
Build: 284, Release 2018.3
Windows 7 Professional Service Pack 1
Intel(R) Xeon(R) CPU E5645
12.0 GB Memory
Message 5 of 16
zdhrichard
in reply to: cwhetten

And I just noticed I do not need the full name. I mean I just need the file name but not the path. Could you help at same time?

Rich

Autodesk Inventor Professional 2018 (64 Bit Edition)
Build: 284, Release 2018.3
Windows 7 Professional Service Pack 1
Intel(R) Xeon(R) CPU E5645
12.0 GB Memory
Message 6 of 16
cwhetten
in reply to: zdhrichard

Try this instead:

 

Dim oAsmComp As ComponentDefinition
oAsmComp = ThisDoc.Document.ComponentDefinition

If oAsmComp.Occurrences.Count = 0 Then
	'do nothing
Else
	Dim oFirstName As String
	oFirstName = oAsmComp.Occurrences(1).ReferencedFileDescriptor.FullFileName

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

End If

 

This code tests if the collection of component occurrences is empty (count = 0).  If so, it does nothing, otherwise it finds the filename (without the path this time).  If you would rather have it to do something, just add the desired code where it says 'do nothing.

 

Please click "Accept as Solution" if this response answers your question.

Cameron Whetten
Inventor 2014

Message 7 of 16
zdhrichard
in reply to: cwhetten

Hello Cameron,

I just have time to test the code which you contributed yesterday, it works great!

Thanks lot.

Rich

Autodesk Inventor Professional 2018 (64 Bit Edition)
Build: 284, Release 2018.3
Windows 7 Professional Service Pack 1
Intel(R) Xeon(R) CPU E5645
12.0 GB Memory
Message 8 of 16
zdhrichard
in reply to: cwhetten

 

Hello Cameron,

I finished my application and the result is perfect now. Thanks lot for your help.

Now I am thinking the situation after I release it to user.

Sorry, I forget mention to you: I am trying to get infromation from the first file in my assembly which includes this rule.

As you know, when we put first file into an assembly, it automaticly put "file name" without extension name and with ":1" as suffix (as "firstblock:1")

So I used your code and took off the extension name and put ":1" to run following snippet.

iProperties.Value("Custom", "BlockHeight") = iProperties.Value(oFirstName, "Custom", "BlockHeight")

 

But maybe there is a chance, user put same file twice into assembly by mistake and later delete the first one. Then the first file name will be: "firstblock:2".

 

Then above snippet will not work well.

 

Do yo know how to just get the display name which without extension name and ":1" or ":2" and so on in assembly? If we can do this, then we do not need to worry how the user put the file now.

 

Appreciate your help again? 

 

Rich

Autodesk Inventor Professional 2018 (64 Bit Edition)
Build: 284, Release 2018.3
Windows 7 Professional Service Pack 1
Intel(R) Xeon(R) CPU E5645
12.0 GB Memory
Message 9 of 16
cwhetten
in reply to: zdhrichard

Hi Rich,

 

Getting the display name is easier than getting the filename.  This should work:

 

Dim oAsmComp As ComponentDefinition
oAsmComp = ThisDoc.Document.ComponentDefinition

If oAsmComp.Occurrences.Count = 0 Then
	'do nothing
Else
	Dim oFirstName As String
	oFirstName = oAsmComp.Occurrences(1).Name 

End If

 

Cameron Whetten
Inventor 2014

Message 10 of 16
zdhrichard
in reply to: cwhetten

Bingo!

All of problem is solved under your help!

Thanks lot, Cameron!

Rich

Autodesk Inventor Professional 2018 (64 Bit Edition)
Build: 284, Release 2018.3
Windows 7 Professional Service Pack 1
Intel(R) Xeon(R) CPU E5645
12.0 GB Memory
Message 11 of 16
zdhrichard
in reply to: cwhetten

Hello Cameron,

Could I ask you one more question?

How Can I get the file type of first file?

I can get it the right letter after I get the fullfilename after run below Snippet which you gave to me.

oFirstName = oAsmComp.Occurrences(1).ReferencedFileDescriptor.FullFileName

 

But do we have a simple way to get it?

I tried to use this snippet, it does not work. Do you have any simple way? Thanks lot for your help in advanced.

 

oFirstType = oAsmComp.Occurrences(1).ReferencedFileDescriptor.FileType 
Rich

Autodesk Inventor Professional 2018 (64 Bit Edition)
Build: 284, Release 2018.3
Windows 7 Professional Service Pack 1
Intel(R) Xeon(R) CPU E5645
12.0 GB Memory
Message 12 of 16
cwhetten
in reply to: zdhrichard

Hi Rich.  You can use the following to get the document type:

 

oFirstType = oAsmComp.Occurrences(1).DefinitionDocumentType

 

This returns an enumerator (enum), which is a numeric constant that represents something.  In this case, it returns a DocumentTypeEnum, which as you might guess represents a type of document.  According to the API help documentation, the different values for DocumentTypeEnum and what they represent are given in the image below:

 

DocumentTypeEnums.PNG

 

 

The following is an example of how you might use this enum in your code:

 

oFirstType = oAsmComp.Occurrences(1).DefinitionDocumentType

If oFirstType = DocumentTypeEnum.kAssemblyDocumentObject Then

    'do some stuff

End If

 

 

Cameron Whetten
Inventor 2014

Message 13 of 16
zdhrichard
in reply to: cwhetten

Thanks lot.

Rich

Autodesk Inventor Professional 2018 (64 Bit Edition)
Build: 284, Release 2018.3
Windows 7 Professional Service Pack 1
Intel(R) Xeon(R) CPU E5645
12.0 GB Memory
Message 14 of 16
zdhrichard
in reply to: zdhrichard

Dim oAsmComp As ComponentDefinition
oAsmComp = ThisDoc.Document.ComponentDefinition
If oAsmComp.Occurrences.Count = 0 Then
'do nothing
Else
Dim oFirstName As String
oFirstName = oAsmComp.Occurrences(1).Name
End If

Could I ask a question about above code?

 

I put this code in my ilogic rule. When I open it fresh, it works fine. But when I worked other projects and then open the file with this rule, it cannot get correct information. I mean 

oAsmComp.Occurrences.Count

does not work well.

I have to close inventor and open the file which with this rule as first open, it works again. 

 

Could someone advise me how to fix it?

 

Thanks a lot in advance.

Rich

Autodesk Inventor Professional 2018 (64 Bit Edition)
Build: 284, Release 2018.3
Windows 7 Professional Service Pack 1
Intel(R) Xeon(R) CPU E5645
12.0 GB Memory
Message 15 of 16

This works great, however, how could I run through all of the Occurrences? Because there is a (1), I am only allowed to get the first file. I want to get multiple. I tried doing: 

 

oFirstName = oAsmComp.Occurrences.ReferencedFileDescriptor.FullFileName

I removed the (1) but it doesn't work

Message 16 of 16

@felix.cortes5K3Y2 

You need a "For loop" for that.

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

Post to forums  

Autodesk Design & Make Report