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: 

iLogic question

16 REPLIES 16
Reply
Message 1 of 17
Anonymous
1107 Views, 16 Replies

iLogic question

Hi everyone, I am looking to change a part's "DisplayName" (from the Name field in the iProperties' Occurrence Tab).

 

I know it is achievable using something like; oRefDoc.DisplayName = "blabla" but it doesn't change the DisplayName of every file (for instance, it doesn't change the DisplayName of a file I had to change it's DisplayName in the Occurrence Tab).

 

As I am working from my top-level assembly (assembly containing my completed machine) I do not want to have to update them all by hands, since the other engineers replace 'em often. I am looking for a way to "force" the DisplayName to change even on customized displayname.

 

Thank you! Any reply are appreciate 🙂

16 REPLIES 16
Message 2 of 17
Anonymous
in reply to: Anonymous

Which version of Inventor are you using?  I do not have an "occurrence" tab, nor do I know what variable you are refering to when you say "displayname".

 

Please clarify.

 

Shane

Message 3 of 17
MegaJerk
in reply to: Anonymous

@stang_shane : He's talking about the occurrence tab that shows up in the iproperties of a part if you right click a component (and select iproperties) in your history tree, while pulled out to the assembly level. 



If my solution worked or helped you out, please don't forget to hit the kudos button 🙂
iLogicCode Injector: goo.gl/uTT1IB

GitHub
Message 4 of 17
MegaJerk
in reply to: Anonymous

Looks like if you go to your –


Assembly.ComponentDefinition.Occurrences.Item(?).Name  


You should be able to Set / Get the name for the occurrence.


As a strange example, the following code would simply change all of the occurrence names to “CrazyName:#”  (that :# incrementing by 1 for each new instance of a part in your assembly).

 

Option Explicit

Public Sub OccurrenceNameChanger()
Dim topDoc As Document
Set topDoc = ThisApplication.ActiveDocument

If (topDoc.DocumentType = kAssemblyDocumentObject) Then

    Dim oDef As AssemblyComponentDefinition
    Set oDef = topDoc.ComponentDefinition
    
    Dim i As Integer
    Dim u As Integer
    
    i = oDef.Occurrences.Count
    
    For u = 1 To i Step 1
    oDef.Occurrences.Item(u).Name = "CrazyName:" & u
    Next
End If
End Sub

 

 

 

***EDIT***

I should note that each occurrence name needs to be different; otherwise you will receive an error. That is why the above example has that numerical increment. Without it, the code would change the first part to CrazyName, then attempt to change the next part to CrazyName as well and immediately stop with an error.



If my solution worked or helped you out, please don't forget to hit the kudos button 🙂
iLogicCode Injector: goo.gl/uTT1IB

GitHub
Message 5 of 17
Anonymous
in reply to: MegaJerk

Thank you for your reply and code MegaJerk (feels weird to write! loll) I tested it all alone and it works wonders!

I was wondering if there was a way to modify it so it fits into my other piece of code.

 

My question is, would I need to do 2 Rules and lunch them one after the other before saving my file or could it be done simultaniously? I am a beginner as to iLogic and codes. Although I understand your logic, I don't see how I could implement it into my piece of code. I don't mind doing it in 2 pieces of codes but I would like to know how it could be done, so I can improve my knowledge of iLogic 🙂

 

My piece of code goes as follow;

 

Dim oAsmDoc As Assemblydocument

oAsmDoc = ThisApplication.ActiveDocument

 

Dim oRefDocs As DocumentsEnumerator

oRefDocs = oAsmDoc.AllReferencedDocuments

 

Dim oRefDoc As Document

For Each oRefDoc In oRefDocs

 

iProperties.Value(System.IO.Path.GetFileName(oRefDoc.FullFileName), "Projet", "Part Number") = Bleh

 

Next

Message 6 of 17
MegaJerk
in reply to: Anonymous

I don't mind helping, but I still don't understand (fully) what it is you're attemping to do. 

 

In your example you are renaming the iproperty Part Number, to “Bleh” for every part in the assembly. Do you then want to update the Occurrence name to say “Bleh:1” ?



If my solution worked or helped you out, please don't forget to hit the kudos button 🙂
iLogicCode Injector: goo.gl/uTT1IB

GitHub
Message 7 of 17
Anonymous
in reply to: MegaJerk

I need to replace the parts iProperties so that the "DisplayName" (Name in Occurrence tab) and the Part Number becomes the filename without extension and then add to the Project Number field the first word of the main assembly's filename.

 

Example consisting of a small assembly example;

 

Main asm's filename is "5901 Folding table.iam"

Part 1's filename is "Folding table - Legs.ipt", Part Number is "Legs" and DisplayName is "Tables Legs"

Part 2's filename is "Table top.ipt", Part Number is "Table top - Folding table" and DisplayName is "Table top:1"

 

In this example I would need to rename the DisplayName (Name in Occurrence tab) to "Folding table - Legs" and "Table top" and the Part Number to the same thing ("Folding table - Legs" and "Table top"; which are the filenames without extansion) and "5901" to all 3 files' Project Number

 

If this is still confusing I could try to give you an example of a "before-after" assembly

Message 8 of 17
MegaJerk
in reply to: Anonymous

Assuming that you never place another instance (occurrence) of a part (.ipt) into the assembly, I think that this might be what you want to do... 

(The Following is an Ilogic Rule (to be run in the top Assembly)) :

Sub Main()

Dim topDoc As Document
topDoc = ThisApplication.ActiveDocument

If (topDoc.DocumentType = kAssemblyDocumentObject) Then

    Dim AssemNameSpace As Long
    AssemNameSpace = InStr(1, topDoc.DisplayName, " ")

    Dim AssemName As String
    AssemName = Left(topDoc.DisplayName, AssemNameSpace - 1)
    
    Dim oDef As AssemblyComponentDefinition
    oDef = topDoc.ComponentDefinition    
    
    Dim i As Integer
    Dim u As Integer
    
    i = oDef.Occurrences.Count
    
    For u = 1 To i Step 1
    
    Dim GrabName As String
    GrabName = oDef.Occurrences.Item(u).ReferencedDocumentDescriptor.DisplayName    
    
    oDef.Occurrences.Item(u).Name = Left(GrabName, Len(GrabName) - 4)
    
    oDef.Occurrences.Item(u).ReferencedDocumentDescriptor.ReferencedDocument.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value = Left(GrabName, Len(GrabName) - 4)
    oDef.Occurrences.Item(u).ReferencedDocumentDescriptor.ReferencedDocument.PropertySets.Item("Design Tracking Properties").Item("Project").Value = AssemName
    
    Next
    
End If
End Sub

 

(The Following is the same code for VBA (Minor Changes in Syntax)) : 

Option Explicit

Public Sub OccurrenceNameChanger()
Dim topDoc As Document
Set topDoc = ThisApplication.ActiveDocument

If (topDoc.DocumentType = kAssemblyDocumentObject) Then

    Dim AssemNameSpace As Long
    AssemNameSpace = InStr(1, topDoc.DisplayName, " ")

    Dim AssemName As String
    AssemName = Left(topDoc.DisplayName, AssemNameSpace - 1)
    
    Dim oDef As AssemblyComponentDefinition
    Set oDef = topDoc.ComponentDefinition
    
    
    
    Dim i As Integer
    Dim u As Integer
    
    i = oDef.Occurrences.Count
    
    For u = 1 To i Step 1
    
    Dim GrabName As String
    GrabName = oDef.Occurrences.Item(u).ReferencedDocumentDescriptor.DisplayName
    
    Dim iPropPartNumber As Property
    Set iPropPartNumber = oDef.Occurrences.Item(u).ReferencedDocumentDescriptor.ReferencedDocument.PropertySets.Item("Design Tracking Properties").Item("Part Number")
    
    Dim iPropProjectName As Property
    Set iPropProjectName = oDef.Occurrences.Item(u).ReferencedDocumentDescriptor.ReferencedDocument.PropertySets.Item("Design Tracking Properties").Item("Project")
    
    oDef.Occurrences.Item(u).Name = Left(GrabName, Len(GrabName) - 4)
    
    iPropPartNumber.Value = Left(GrabName, Len(GrabName) - 4)
    iPropProjectName.Value = AssemName
    
    Next
    
End If
End Sub

 

 

 

So I made an assembly called 4458 Test Assembly (File name of 4458 Test Assembly.iam), that contained two parts (Folding table-Legs.ipt  & Table top.ipt). They were both inside of the assembly only once, and once that rule ran, the iproperties for the files were as follows  : 

Part (Folding table-Legs.ipt) :  Part Number = Folding table-Legs
Project = 4458

 

Part (Table top.ipt) :PartNumber = Table top

Project = 4458

 

**** The code above will error out as soon as you place another instance of the same part file into the assembly! I will work tomorrow on fixing that. As of now. Sleep must happen! ****  

 



If my solution worked or helped you out, please don't forget to hit the kudos button 🙂
iLogicCode Injector: goo.gl/uTT1IB

GitHub
Message 9 of 17
Anonymous
in reply to: Anonymous

Thank you MegaJerk, will it work with edited DisplayName, Projects and Part Numbers or only "standard"(Inventor's default) fields? I can't test it as of right now since my desktop cpu died on me last night but as soon as I buy the part I'll repair it and run your iLogic rule! 

 

From what I see you set the fields using the "default" DisplayName, which should in fact be the filenames of the asm's and ipt's (since all of our filenames should be correct, we prefer to use them. our old engineers used to modify the fields for our drawings at that time but we used the parts on various projects which made them erroneous)

 

BTW, thanks for taking the time 🙂 I hope to fix my cpu as soon as possible to test it out 🙂 (darn power supplies)

Message 10 of 17
MegaJerk
in reply to: Anonymous

Ah yes. I will go through and see about changing that to the filename. I can see where that would be a problem. When you get your computer fixed, try it out, and for kicks, post that before / after assembly if things still aren't up to snuff. 

 



If my solution worked or helped you out, please don't forget to hit the kudos button 🙂
iLogicCode Injector: goo.gl/uTT1IB

GitHub
Message 11 of 17
Anonymous
in reply to: MegaJerk

Hi, I tested your piece of code (not the VBA but the other one) and it seems to NOT be working. It returns me an error.

 

Here's the error; 

 

Error in rule; Rule0, in document: 5139 z.iam

Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))

 

I will try some things but I don't believe I will be able to achieve it on my own, it seems pretty "advanced" and I'm only a beginner. If I manage to pull it off, I will show the piece of code I used. I will be waiting for your replies (MegaJerk or any other member! Any help is appreciated!) 

 

 

-----------

I had 3 parts with the same filename, which led to this error.

All in all, it seems to work fine although it is written "DisplayName" everywhere, which seems odd to me, If you could explain it would be really nice since it seems so odd to me that the "DisplayName" is the same as the "FileName" even though it is manually (or not) changed!! :S

As you stated, it would need to be able to handle multiple instances of one file, which for now is semi-useful (handling only 1 instance of a file). Thanks a lot too MegaJerk by the way 🙂

Message 12 of 17
MegaJerk
in reply to: Anonymous

Alright. I believe that this should do the trick. No errors (when I tested it). Place this in a fresh rule file, and let it rip. 

Dim openDoc As Document
openDoc = ThisDoc.Document
'Set openDoc = ThisApplication.ActiveDocument

Dim docFile As Document

If openDoc.DocumentType = 12291 Then

    For Each docFile In openDoc.AllReferencedDocuments

        If docFile.DocumentType = 12290 Then

            Dim assemblyDoc As AssemblyDocument
            assemblyDoc = openDoc

            Dim assemblyDef As AssemblyComponentDefinition
            assemblyDef = assemblyDoc.ComponentDefinition

            Dim partDoc As PartDocument
            partDoc = ThisApplication.Documents.Open(docFile.FullFileName, False)
            
            Dim partOcc As ComponentOccurrencesEnumerator
            partOcc = assemblyDef.Occurrences.AllReferencedOccurrences(partDoc)
            
            Dim FNamePos As Long
            FNamePos = InStrRev(docFile.FullFileName, "\", -1)
            
            Dim docFName As String
            docFName = Mid(docFile.FullFileName, FNamePos + 1, Len(docFile.FullFileName) - FNamePos - 4)
			
            Dim AFNamePos As Long
            AFNamePos = InStrRev(openDoc.FullFileName, "\", -1)
            
            Dim assemblyFName As String
            assemblyFName = Mid(openDoc.FullFileName, AFNamePos + 1, Len(openDoc.FullFileName) - AFNamePos - 4)
            
            Dim assemblyIPName As String
            assemblyIPName = Left(assemblyFName, InStr(1,assemblyFName, " ")-1)
            
            Dim i As Long            
            
                For i = 1 To partOcc.Count Step 1
                
                    partOcc.Item(i).Name = docFName & ":" & i
                    
                Next
				
			iProperties.Value(docFile.DisplayName, "Project", "Project") = assemblyIPName
			iProperties.Value(docFile.DisplayName, "Project", "Part Number") = docFName
			

        End If
        
    Next
    
    Else
    
    'MessageBox.Show("You must have a valid Assembly document open before using this code!", "File Type Mismatch!",MessageBoxButtons.OK,MessageBoxIcon.Exclamation)
    
End If

 

 

Run from your main assembly, and with any luck, it does what you would like! 

Peace!  



If my solution worked or helped you out, please don't forget to hit the kudos button 🙂
iLogicCode Injector: goo.gl/uTT1IB

GitHub
Message 13 of 17
Anonymous
in reply to: Anonymous

Hi, sorry for the delay, I havent forgotten you and this marvelous piece of iLogic code!

 

I have been able to test it this morning and it would work, but only with parts, it wouldnt replace the iProps of asm within the main asm. Other than that, it is perfect 🙂 I even tested the Message.Box warning popup and it works 🙂

 

Message 14 of 17
MegaJerk
in reply to: Anonymous

Ah! Good good good, well... almost! I will revise this when I get a chance. I've been off from work for the past week so I need to play a little catch-up before I can mess with the code, but I believe that I already have an answer to the sub-assembly problem. 

Just for clarification, what are the specific values for the iproperties that you would like the sub-assembly to have?  



If my solution worked or helped you out, please don't forget to hit the kudos button 🙂
iLogicCode Injector: goo.gl/uTT1IB

GitHub
Message 15 of 17
Anonymous
in reply to: MegaJerk

Actually, all sub-assemblies should have the same iProps as the parts (Project and Part Number) but also a custom property called "AsmType" where it says that it is a weldment asm (value of AsmType; W) or regular asm (value of AsmType; R) (if that is possible, we can keep writing it if it isn't).

 

If you wouldn't mind AND have the time, would it be possible to detect if an .ipt is either a sheet metal or a regular part? If it is possible, could there be a custom property called "PartType" where it says that it is a laser(sheet metal) part (value of "PartType"; L) or shop(regular part) (value of "PartType"; S).

 

Hope you had a great week and wish you an happy new year! I could barely walk for the holidays since I walked on a part at the shop, sucks! thats why I couldn't reply much faster, my bed's far from my computer. BTW, sorry if I am asking too much but this would take forever for me to do.

 

 

If you want to have a little summary of what I'm actually asking, here it is;

 

All parts should have a custom iProp called "PartType"

-------"PartType" for SHEET METAL PARTS equals "L"

-------"PartType" for REGULAR PARTS equals "S"

 

All assembly should have a custom iProp called "AsmType"

-------"AsmType" for WELDMENT ASM equals "W"

-------"AsmType" for REGULAR ASM equals "R"

 

All parts and assemblies (including main assembly) should have as

-------"Project #" equals 1st word of main assembly

-------"Part Number" equals their filename without extension

Message 16 of 17
MegaJerk
in reply to: Anonymous

In the spirit of ‘teaching a man to fish’, I have revised my original code one final time, but without the very last few things you requested. Though this means that it is up to you to implement those final tag-alongs (custom iProperty, etc., etc.), I feel like you should be able to given that I’ll still be monitoring this thread for any other code suggestions you might need. Actually, for the most part, what you want is already (almost) there. If you look at the iproperties à Project, you’ll notice a category called Subtype. Now all you have to do is use that to populate something else 😄

Any ways. Here is a code revision I made after I found a few problems with my last iteration. The code would error out if the original top level assembly file did NOT have any spaces in it, and it would also freak out if you had suppressed any parts that were of an occurrence number lower than an occurrence of the same part that was not suppressed.

I have now tested this code several times, so with any luck it should not break unless there are some scenarios that I simply overlooked.

 

 

 

 

Sub Main()
Dim openDoc As Document
openDoc = ThisDoc.Document
'Set openDoc = ThisApplication.ActiveDocument

Dim docFile As Document

	If openDoc.DocumentType = 12291 Then

	Dim TopAssemblyIPName As String 
		
		For Each docFile In openDoc.AllReferencedDocuments
	
			If docFile.DocumentType = 12290 Or docFile.DocumentType = 12291 Then 			
				
				Dim assemblyDoc As AssemblyDocument
				assemblyDoc = openDoc
	
				Dim assemblyDef As AssemblyComponentDefinition
				assemblyDef = assemblyDoc.ComponentDefinition
				
				Dim partDoc As Object
				
					If docFile.DocumentType = 12290 Then
						partDoc = ThisApplication.Documents.Open(docFile.FullFileName, False)
						Else					
						partDoc = ThisApplication.Documents.Open(docFile.FullFileName, False)
					End If 
				
				Dim partOcc As ComponentOccurrencesEnumerator
				partOcc = assemblyDef.Occurrences.AllReferencedOccurrences(partDoc)
				
				Dim FNamePos As Long
				FNamePos = InStrRev(docFile.FullFileName, "\", -1)
				
				Dim docFName As String
				docFName = Mid(docFile.FullFileName, FNamePos + 1, Len(docFile.FullFileName) - FNamePos - 4)
				
				Dim AFNamePos As Long
				AFNamePos = InStrRev(openDoc.FullFileName, "\", -1)
				
				Dim assemblyFName As String
				assemblyFName = Mid(openDoc.FullFileName, AFNamePos + 1, Len(openDoc.FullFileName) - AFNamePos - 4)
				
				TopAssemblyIPName = assemblyFName
				
				Dim assemblyIPName As String
				
				Try 
					assemblyIPName = Left(assemblyFName, InStr(1,assemblyFName, " ")-1)
				Catch 
					assemblyIPName = assemblyFName
				End Try
				
				Dim i As Long            
				
					For i = 1 To partOcc.Count Step 1
					
						NameNumber(i,docFName,1,partOcc)
					
							If i = 1 Then
								iProperties.Value(docFName & ":" & i, "Project", "Project") = assemblyIPName
								iProperties.Value(docFName & ":" & i, "Project", "Part Number") = docFName
							End If	
							
					Next
					
			End If 
			
		Next
	
		openDoc.DisplayNameOverridden = False
		iProperties.Value("Project", "Project") = TopAssemblyIPName
		iProperties.Value("Project", "Part Number") =TopAssemblyIPName
		
	End If
	
End Sub

Sub NameNumber(nnNumber As Long, nnName As String, nnInc As Long, nnPartOcc As ComponentOccurrencesEnumerator)
	Try 
		nnPartOcc.Item(nnNumber).Name = nnName & ":" & nnInc
	Catch
		NameNumber(nnNumber, nnName, nnInc + 1, nnPartOcc)
	End Try
End Sub

 



If my solution worked or helped you out, please don't forget to hit the kudos button 🙂
iLogicCode Injector: goo.gl/uTT1IB

GitHub
Message 17 of 17
Anonymous
in reply to: MegaJerk

Thanks! I will give it a shot as soon as I have a chance! for now I am kinda busy but as soon as I can work with that piece of code, I will test it, reply and thank you again! Sorry for the long delay, I couldn't log in on the forum... it was telling me my password and username were wrong although they are entered automatically... lol anyways, thanks again and hope to be able to reply soon ;o)

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

Post to forums  

Autodesk Design & Make Report