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

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

Anonymous
Not applicable
5,287 Views
15 Replies
Message 1 of 16

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

Anonymous
Not applicable

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.

0 Likes
Accepted solutions (4)
5,288 Views
15 Replies
Replies (15)
Message 2 of 16

cwhetten
Advisor
Advisor
Accepted solution

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

0 Likes
Message 3 of 16

Anonymous
Not applicable

Great! It is what I want.

Thanks lot.

0 Likes
Message 4 of 16

Anonymous
Not applicable

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.

0 Likes
Message 5 of 16

Anonymous
Not applicable

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?

0 Likes
Message 6 of 16

cwhetten
Advisor
Advisor
Accepted solution

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

Anonymous
Not applicable

Hello Cameron,

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

Thanks lot.

0 Likes
Message 8 of 16

Anonymous
Not applicable

 

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? 

 

0 Likes
Message 9 of 16

cwhetten
Advisor
Advisor
Accepted solution

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

0 Likes
Message 10 of 16

Anonymous
Not applicable

Bingo!

All of problem is solved under your help!

Thanks lot, Cameron!

0 Likes
Message 11 of 16

Anonymous
Not applicable

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 
0 Likes
Message 12 of 16

cwhetten
Advisor
Advisor
Accepted solution

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

0 Likes
Message 13 of 16

Anonymous
Not applicable

Thanks lot.

0 Likes
Message 14 of 16

Anonymous
Not applicable
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.

0 Likes
Message 15 of 16

felix.cortes5K3Y2
Advocate
Advocate

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

0 Likes
Message 16 of 16

ReneRepina
Collaborator
Collaborator

@felix.cortes5K3Y2 

You need a "For loop" for that.

0 Likes