Replacing the Parts List (from an external file) with iLogic

Replacing the Parts List (from an external file) with iLogic

istehno
Participant Participant
1,903 Views
9 Replies
Message 1 of 10

Replacing the Parts List (from an external file) with iLogic

istehno
Participant
Participant

The Part List has been changed on the drawings. We need a simple program to replace the Parts List on an open drawing without having to manually copy the style. We can replace the frame, but not the parts list.

Accepted solutions (1)
1,904 Views
9 Replies
Replies (9)
Message 2 of 10

WCrihfield
Mentor
Mentor

Hi @istehno.  Can you please explain the situation in more detail.  How has the existing PartsList been changed?  Why does it need to be replaced, instead of edited somehow?  What do you mean by manually copying the style...what type of style, a PartsListStyle?  You mentioned being able to replace the frame, but what is the frame?  Do you mean that the main assembly of the drawing has changed to a different document, but the PartsList did not update accordingly, and now you want to point the PartsList to the new assembly document?  What type of external file are you talking about?  Just trying to clarify the details, so we can better understand, and be able to offer more accurate responses or solutions.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 10

istehno
Participant
Participant

Hi @WCrihfield 

We have old drawings (Old frame and old BOM)

When reusing the drawing, we would like to update the entire drawing with one button. We can replace the frame, but we can't get the parts list.

Column mapping has changed in the BOM.

for example (Add "COMMENTS" and Remove "DESCRIPTION")

istehno_0-1698429497526.png

 

Two options came to mind:
1) Import from an external file.
2) Editing the parts list in the active drawing

0 Likes
Message 4 of 10

WCrihfield
Mentor
Mentor

Hi  @istehno.  If you have an existing PartsListStyle, like in your current drawing template file, or in another old drawing that you have recently updated the way you want it, you can save that style up to the global styles library, either manually, or by code to get the process started out right.

Then, in your older drawings:

  • If the new style has a different name than the one in the old drawings, you can copy that new style down from the global style library into the old drawing, then set all existing PartsLists to use that new style.
  • Or, if the new and old style have the same name, and you just want the new one to overwrite the old one in the old drawings, then you can update the existing style in the old drawings to match the one in the global styles library.

There are multiple ways to accomplish these types of tasks by code, but the second scenario mentioned above is also very quick & easy to do manually with a few clicks, without any code.  When the new style has the same name as the old style in the old drawings, and the new style has been saved up to the global styles library, then in the old drawings, you can go to the Manage tab, Styles and Standards panel, and click on the Update tool right in the ribbon.  That will quickly open a small dialog where you can click the [Yes to All] button, then the [OK] button, to get the job done quickly.

 

If it is the first scenario (different style names), then that can get a bit more complicated.  The manual process would be to:

  1. Open the Styles Editor dialog
    1. Make sure the filter in the top right corner is set to "All Styles" (instead of "Local Styles" or "Active Standard").
    2. Select the new style in the list on the far left.
    3. Right-click on it, and choose "Save in Document" option. (makes a local copy of it)
    4. Select the active "Standard" style (within top left group)
    5. Click on the "Object Defaults" tab on the right.
    6. Scroll down to where "Parts List" is on the Object Type column, then change the style for it to the new one.
    7. Now click the [Save] button at the top of the dialog, then the [Save and Close] button at the bottom right, to close the dialog.
  2. Go to the Annotate tab.
  3. Select your PartsList(s) in the sheet
  4. On the Format panel (in the ribbbon), choose the new PartsListStyle, which will change the selected PartsList to using the new style.

I'm leaving for the day now, but if I have time on Monday, I may try to supply a code for whichever process suits your needs best then.  Have a good weekend.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 10

istehno
Participant
Participant

Hi @WCrihfield. Thank you for your help. 

Manual change is quite simple, but it's about 1000+ drawings that need to be modified gradually (we modify the drawings gradually when we make a change, we can't release this change in bulk)

 

I looked for two options but couldn't find any information on either.
1-creating a new parts list using iLogic (new name parts list, selected columns)
2-export of the existing parts list (I don't know if it is exported from the template or from the exported *.styxml file)

 

for example (Change stamp)

ThisDrawing.ResourceFileName = “\\path\Drawing.idw”
ActiveSheet.TitleBlock = “New-Stamp”

Dim oTemplateDoc As Document
For Each oTemplateDoc In ThisApplication.Documents
    If oTemplateDoc.FullFileName = "\\path\Drawing.idw" Then
        oTemplateDoc.Close
        Exit For
    End If
Next

 or change parts list (but The new parts list must be imported in the active document)

Dim openDoc As Document
   'assumes a drawing document is active.
    Dim oDrawDoc As DrawingDocument
    oDrawDoc = ThisApplication.ActiveDocument
        
    'Set a reference to the active sheet.
    Dim oSheet As Sheet
    oSheet = oDrawDoc.ActiveSheet

    'Look for partlist within drawing. End rule, if it doesn't exist.
    'say there is a Partslist on the sheet.
    Dim oPartslist As PartsList
    oPartslist = oSheet.PartsLists(1)

    If oSheet.PartsLists(1) IsNot Nothing Then

        'set parts list to a specific style
        oPartslist.Style = oDrawDoc.StylesManager.PartsListStyles.Item("Parts List (ISO)")

     End If 

 

 

Message 6 of 10

WCrihfield
Mentor
Mentor

Hi @istehno.  You still did not answer my main question about whether or not the old PartsListStyle is the same name as the newer / updated PartsListStyle.  Knowing that detail is important to which code process to use.  The first code in your last response will not work for this process.  That only works for TitleBlock & Border related issues, not PartsListStyle issues.  The second code in your last response is closer, but still lacks multiple needed steps.  I do not believe that you need to actually 'Export' the new PartsListStyle, you just need to 'save to style library', to get things started.  There are two main ways to save the updated style to the library manually (and 1 way by code).  Manually, when the Styles & Standards editor is open, and you have the updated PartsListStyle selected, right-click on it, then choose "Save to Style Library" option.

WCrihfield_0-1698672577304.png

You will only need to do this one time.  All other processes are to 'push' that library style to the older drawings.  But as you can see in that image, another option there is called "Save in Document".  That is only for when you want to save a local copy of a library style into your local document for use.  This step is needed if the style only exists in the library, because you can not use a library style directly in your drawing...there must be a local copy of the style, then you can apply that local style to stuff in your drawing.  However, also notice that there is another option shown in the red circled area of the image called "Update Style".  This is for when the same style exists in the library and in the document, but you want the style in the document to match the style in the library.  We can do any of these things by code, but if you would like me to help you write the code for automating the process, I first need to know which process we need to be using.  If the old and new style have the same name, then we need to use the "Update Style" process, which is much simpler to deal with.  If the old and new styles have different names, then we need to copy the updated library style down to the old drawing, then set that local copy as the 'default' style, and loop through every PartsList in the drawing, setting them to the new local style.

 

Edit:  The other option I did not mention above is the "Replace Style ..." option.  If the old and new styles have different names, and you want the new one to replace the old one, then you would use that route also, which may actually be a bit cleaner than just trying to use a different style altogether.  That option can be done by code too, if you want.  The method for it is just under the DrawingStylesManager object, called ReplaceStyles.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 7 of 10

istehno
Participant
Participant

Now we use the old one and the new one with the same name.

0 Likes
Message 8 of 10

WCrihfield
Mentor
Mentor
Accepted solution

OK.  Here is an iLogic rule you can try running while one of the old drawings is active for that task.  It will try to find a specific PartsListStyle by its name (either in the style library, or in the document itself).  You will need to change the style name being specified within the code before running it.  If it is not found, it will let you know (one way or another), then exit the rule.  Then it checks if that style is only in the library, in both the library and the document, or only in the document, and reacts differently for each scenario (does nothing if only found in document - not expected).  If found in the library only, it creates a copy of it in the document, and captures that local copy.  If in both, it makes sure the local version is up to date with the one in the library.  Then it loops through every sheet, and every PartsList in the drawing, forcing them to use that style.  Not 100% sure if that last step may be necessary, but it's there if needed.  Then it updates the drawing if it is needed.  Then saves the drawing, if needed.

Sub Main
	If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
		MsgBox("A Drawing document must be active for this code to work. Exiting.", vbCritical, "")
		Return 'exit rule if it is not a drawing, to prevent errors later
	End If
	Dim oDDoc As DrawingDocument = ThisDoc.Document
	Dim oStylesMgr As DrawingStylesManager = oDDoc.StylesManager
	Dim oPLStyles As PartsListStylesEnumerator = oStylesMgr.PartsListStyles
	Dim oPLStyle As PartsListStyle = Nothing
	Dim sPLStyleName As String = "PartsListStyleName" '<<< CHANGE THIS >>>
	Try
		oPLStyle = oPLStyles.Item(sPLStyleName)
	Catch 'what to do if that fails
		MessageBox.Show("Cound not find PartsListStyle named: ", "PartsListStyle Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error)
		'Logger.Error("Could not find PartsListStyle named: " & sPLStyleName)
		Return
	End Try
	If oPLStyle Is Nothing Then Return 'if not found, exit routine or rule
	If oPLStyle.StyleLocation = StyleLocationEnum.kLibraryStyleLocation Then
		Try
			oPLStyle = oPLStyle.ConvertToLocal
		Catch 'what to do if that fails
			MessageBox.Show("Error converting library style to local style!", "Error Converting To Local", MessageBoxButtons.OK, MessageBoxIcon.Error)
			'Logger.Error("Error converting library style to local style!")
		End Try
	ElseIf oPLStyle.StyleLocation = StyleLocationEnum.kBothStyleLocation Then
		'compare this style against library version with same name, if different will return False
		If oPLStyle.UpToDate = False Then 'they were different somehow
			oPLStyle.UpdateFromGlobal 'update this style to match the library style with same name
		End If
	ElseIf oPLStyle.StyleLocation = StyleLocationEnum.kLocalStyleLocation Then
		'what to do when the style is only found in local document, not in library (may never happen)
	End If
	'loop through all PartsLists in drawing to make sure they are updated
	Dim oSheets As Inventor.Sheets = oDDoc.Sheets
	For Each oSheet As Inventor.Sheet In oSheets
		Dim oPLists As PartsLists = oSheet.PartsLists
		If oPLists.Count = 0 Then Continue For 'if no PartsLists, skip to next sheet
		For Each oPList As PartsList In oPLists
			Try
				oPList.Style = oPLStyle
			Catch 'what to do if that fails
				Logger.Error("Error setting PartsList.Style to updated PartsListStyle!")
			End Try
		Next 'oPList
	Next 'oSheet
	If oDDoc.RequiresUpdate Then oDDoc.Update2(True)
	If oDDoc.Dirty Then oDDoc.Save2()
End Sub

A similar process can be used for most other types of styles.  In fact a lot of folks loop through all styles (of every type), and attempt to make sure they are updated to match the library.  I hope this will work OK for you.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 9 of 10

simon
Participant
Participant

This I logic rule works, but the list  is not updated. I need to select the style to update. It works if I add this.

		For Each oPList As PartsList In oPLists
			Try
				oPList.Style = oDDoc.StylesManager.PartsListStyles.Item(1)
				oPList.Style = oPLStyle

Only he is not aligned with the the title block then.

0 Likes
Message 10 of 10

WCrihfield
Mentor
Mentor

Hi @simon.  Applying styles to drawing annotations is not always as simple as it seems like it should be.  Sometimes just setting the overall Style object does not have the effect you expect immediately, usually due to existing 'overrides' to some of the individual aspects of the annotation.  So, if setting the Style to another Style, then to the Style you want works better for you, then that's certainly OK.  As for the PartsList moving from its original location, due to the Style changes, that is a tough one to account for.  The PartsList only has the one property (PartsList.Position) for controlling its location, and it is just a 'transient' 2D point type value, instead of any 'real' geometry.  And I am not aware of any 'constraint' that we can apply to a PartsList to 'lock in in place'.  The traditional code based process for handling something like that, would be to record the value of that Position property to a variable before making the changes, then try to set the value of that Position property again, to the same 'recorded' value, after the changes have been made.  This does not always work out as planned though.  There are multiple factors involved, such as where on the PartsList is this 'Position' (top left, top center, top right, center, Bottom left, etc.).  Next is how you may have had it 'docked' before.  Was it docked to upper left corner of border, upper right of border, lower right, lower left, somewhere between left/right/top/bottom?  All that has to be taken into account when 'setting' the Position.  If the 'position' is always at top/left corner, but you want it docked to bottom right of border (above title block), then you must know the 'size' (Height & Width) of the PartsList, know the exact coordinates of that lower right location, then do the calculations.  Sometimes you can use the PartsList.RangeBox to help you determine the 'current' lower left, and upper right points of the PartsList, before doing the required calculations.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes