Trying to populate CNC program names into parts and assemblies

Trying to populate CNC program names into parts and assemblies

mitchell6LPHR
Participant Participant
731 Views
7 Replies
Message 1 of 8

Trying to populate CNC program names into parts and assemblies

mitchell6LPHR
Participant
Participant

We work with wood paneling systems and casegoods. We nest panels together when we run them through the CNC whenever we can. To nest everything together in the optimal way, we make a simple Inventor assembly containing the laid out panels and create the program from that. I want each part file and assembly file to contain a Part Number (as usual) in addition to a Program Number (as a custom property). I am updating my part and assembly templates to contain iLogic code to populate all of this automatically. I just want an automatic "Program Number" property that updates itself with minimal intervention that I can place into my drawing template and trust that it refers to the correct CNC program.

 

Here's an example. I have three parts:

 

8001.ipt

8002.ipt

8003.ipt

 

I decide they should be nested together to be run through the CNC. I put them into an assembly together and name the assembly 8001N.iam. At this point, I want all four files--8001, 8002, 8003, and 8001N, to have their individual "Program Name" properties updated to match the assembly filename, which is 8001N ("Main Program" in the iLogic code). Here is what I have so far--it doesn't throw any errors but it doesn't do anything, either.

 

Sub Main Program()
    MainProgram=iProperties.Value("Project", "Part Number")

    Dim oDoc As AssemblyDocument
    oDoc = ThisApplication.ActiveDocument

    Call SetChildProgram(oDoc, MainProgram)   
End Sub

Sub SetChildProgram(oDoc As Document, MainProgram As String)
    If oDoc.ReferencedDocuments.Count > 0 Then
        For Each oSubDoc In oDoc.ReferencedDocuments
             If oSubDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject
                  oThisDwgNumber = oSubDoc.PropertySets("Custom")("Program").Value
                  Call SetChildProgram(oSubDoc, oThisDwgNumber)
             Else
                  
				  Try
                      oSubDoc.PropertySets("Custom")("Drawing Number").Value = MainProgram
                  Catch
                      oSubDoc.PropertySets("Custom").Add(MainProgram, "Drawing Number")
                  End try
              End if
         Next
     End if
End Sub

 Hopefully it is clear enough what I am trying to accomplish. Any advice is appreciated. I'd be happy to give more examples.

0 Likes
Accepted solutions (1)
732 Views
7 Replies
Replies (7)
Message 2 of 8

mitchell6LPHR
Participant
Participant

Just to clarify: Each part should end up with a custom property named "Program" that gets populated with a text string that matches the overarching assembly's filename. I have an if/then statement before all this dictating whether an assembly is or isn't a CNC nest so I don't have to worry about populating things where I don't need to.

0 Likes
Message 3 of 8

WCrihfield
Mentor
Mentor

Just trying to clarify all of this in my head.  Does the 'Part Number' iProperty always contain the file name (without file path or file extension)?  I see two different 'custom' iProperties involved in your code...("Drawing Number" and "Program").  What is the difference between those two?  What are each of them supposed to contain as a Value?  Are they both used in all Parts & all Assemblies?  I was under the impression that the only two properties involved would be the Part Number and Program, and the Program would always contain the Part Number of the parent assembly (if there is one).

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 4 of 8

A.Acheson
Mentor
Mentor
Accepted solution

Here is what I think you are needing. I have just placed the custom iproperty check into a sub routine so you can call when you need it, As you are using All referenced documents this will be called once per document. So as it is now the mainprogram value will  be added  to all files.

 

Sub Main 
	Dim MainProgram As String
    MainProgram = iProperties.Value("Project", "Part Number")

    Dim oDoc As AssemblyDocument
    oDoc = ThisApplication.ActiveDocument
	
    Call SetChildProgram(oDoc, MainProgram)   
End Sub

Sub SetChildProgram(oDoc As Document, MainProgram As String)
        For Each oSubDoc In oDoc.ReferencedDocuments 
			Call UpdateCustomiProperty(oSubDoc,"Program",MainProgram) 
         Next
End Sub

		   
Private Sub UpdateCustomiProperty(ByRef Doc As Inventor.Document, _
ByRef PropertyName As String, _
ByRef PropertyValue As Object)
'https://modthemachine.typepad.com/my_weblog/2010/02/custom-iproperties.html
'https://modthemachine.typepad.com/my_weblog/2010/02/accessing-iproperties.html
    ' Get the custom property set.
    Dim customPropSet As Inventor.PropertySet
    customPropSet = Doc.PropertySets.Item( _
   "Inventor User Defined Properties") 

    ' Get the existing property, if it exists.
    Dim prop As Inventor.Property = Nothing
    Dim propExists As Boolean = True
    Try
    prop = customPropSet.Item(PropertyName)
    Catch ex As Exception
    propExists = False
    End Try 

    ' Check to see if the property was successfully obtained.
    If Not propExists Then
    ' Failed to get the existing property so create a new one.
    prop = customPropSet.Add(PropertyValue, PropertyName)
    Else
    ' Change the value of the existing property.
    prop.Value = PropertyValue
    End If
End Sub   

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 5 of 8

mitchell6LPHR
Participant
Participant

Thank you so much. This looks like what I need but something still isn't working the way I want--the part files are not acquiring the program name into their custom properties. But I will still play around with this, because I know it's super close.

 

But thank you again, I really appreciate you and the other commenter for taking the time to look at this.

0 Likes
Message 6 of 8

A.Acheson
Mentor
Mentor

Are the parts read only? If they are it will error out on adding the custom iproperty. Anymore trouble let us know.

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 7 of 8

mitchell6LPHR
Participant
Participant

Actually - it works perfectly, you nailed it! I had to make some adjustments to another rule that I had running. Thank you so much, you have no idea how much time you've just saved me.

0 Likes
Message 8 of 8

A.Acheson
Mentor
Mentor

No problem, I think you were going wrong with using ilogic terminology (Custom) mixed in with the API property sets. It is a mine field but those links I left in there show you some good samples. The API help also has some good info just search property sets.  I would recommend you declare the variables and the objects such as  property set, property sets so that  you can  make use of the drop downs of properties and methods that come up after declaring them.

 

Note also the ilogic iproperty is specific only to the document your in unless you assign a different document reference. This  why it can be easier to got the property sets route as you have more control over when it creates an iproperty and being able to read read only iproperties.

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes