Change parts list style based on part used in sheet 2

Change parts list style based on part used in sheet 2

SharkDesign
Mentor Mentor
807 Views
6 Replies
Message 1 of 7

Change parts list style based on part used in sheet 2

SharkDesign
Mentor
Mentor

Sheet 1 has an iam file on it and a parts list.

Everything is good.

 

Sheet 2 has a ipn file on it and a parts list.

I want this parts list to use a different style.

 

I've got the below code, but it doesn't work. I believe it's because it's checking the file used on sheet 1 to see it's an ipn instead of sheet 2. I think this is probably the problem, but I don't know how to fix it:

 

oType = oDoc.DocumentSubType.DocumentSubTypeID

The code works if I put the ipn on sheet 1.

 

Another question is, what does the (1) mean in this line?

oSheet.PartsLists(1)

 

 

Sub Main

Dim oDoc As Document
oDoc = ThisDrawing.ModelDocument



Dim oType As String
oType = oDoc.DocumentSubType.DocumentSubTypeID

'is View 1 a part
If oType = "{76283A80-50DD-11D3-A7E3-00C04F79D7BC}" Then
	PLstyle 'run function
      Return
Else 
	Logger.Info("Presentation File Not Found", filename)


End If

End Sub



Function PLstyle
	
	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 = ThisDrawing.Sheet("Sheet:2")'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("Exploded Parts List")
	Else
		Logger.Info("Parts list not found.", filename)

     End If 
	 
 End Function

 

  Inventor Certified Professional
0 Likes
Accepted solutions (3)
808 Views
6 Replies
Replies (6)
Message 2 of 7

WCrihfield
Mentor
Mentor

Hi @SharkDesign.  You are correct.  In a drawing, it is only expecting there to be one model document being represented within.  The term you used 'ThisDrawing.ModelDocument will always return the model document being represented within the first drawing view in the drawing that is representing a model.  Then most folks also have their drawing title block text linked to the model document's iProperties, or have the drawing set up to automatically copy model iProperties over to the drawing when that first model view is placed, then the title block is showing drawing iProperties.  These things will always be pointing to that 'first' model document, in the 'first' view that is representing a model in the drawing.

 

  Having multiple different model documents being represented throughout the drawing is always much more challenging to deal with from iLogic, but not impossible.  You basically need to retrieve the model document reference from each drawing view on each sheet to know which view is representing which model document, when done this way.

And oSheet.PartsLists(1) is the same as oSheet.PartsLists.Item(1), which is basically trying to get the 'first' Parts List on that sheet (if there is one).

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 7

JhoelForshav
Mentor
Mentor
Accepted solution

Hi @SharkDesign 

Sounds like you want something like this?

Dim oDrawDoc As DrawingDocument = ThisDrawing.Document
For Each oSheet As Sheet In oDrawDoc.Sheets
	If oSheet.PartsLists.Count > 0
		Dim oPartsList As PartsList = oSheet.PartsLists(1)
		Dim oView As DrawingView = oSheet.DrawingViews(1)
		Select Case oView.ReferencedDocumentDescriptor.ReferencedDocument.DocumentType
			Case DocumentTypeEnum.kAssemblyDocumentObject
				'The sheets first view is of an assembly, maybe you want to do something here?
			Case DocumentTypeEnum.kPartDocumentObject
				'The sheets first view is a part, set style of partslist
				oPartsList.Style = oDrawDoc.StylesManager.PartsListStyles.Item("Exploded Parts List")
		End Select
	Else
		Logger.Info("No partslist in " & oSheet.Name)
	End If
Next

The code loops through the sheets in your drawing and if a partslist exists and the first view of the sheet is a part it sets the partslist style to "Exploded Parts List". This style has to exist in the documents styles though for it to work.

 

The (1) youre asking about is the index of the partslist, since in multiple partslists can exist in the same sheet.

Message 4 of 7

WCrihfield
Mentor
Mentor
Accepted solution

Here is how I was going to change your code.  Keep in mind that any time you just want a seperate custom routine to do something, without returning anything, you use a Sub.  Then if you need that routine to return something you use a Function.  And when creating a custom function, you need to define what type of object it is supposed to return right after where you define the input variables.

 

Sub Main
	'Dim oDoc As Document = ThisDrawing.ModelDocument
	Dim oDDoc As DrawingDocument = ThisDrawing.Document
	Dim oMDoc As Document
	'get first view on second sheet
	Dim oSheet As Sheet = oDDoc.Sheets.Item(2)
	Dim oView As DrawingView = oSheet.DrawingViews.Item(1)
	Try
		oMDoc = oView.ReferencedDocumentDescriptor.ReferencedDocument
	Catch
		MsgBox("There was no model document being represented by that view.", , "")
		Exit Sub
	End Try
	'only continue if this is a Presentation document
	If oMDoc.DocumentType <> DocumentTypeEnum.kPresentationDocumentObject Then
		Logger.Info("Presentation File Not Found")
		Exit Sub
	End If
	'now check for the Parts List on that sheet, if there is one
	Dim oPList As PartsList
	If oSheet.PartsLists.Count > 0 Then
		oPList = oSheet.PartsLists.Item(1)
		'now supply that Parts List to the Sub, along with the Style name, so it can change its Style
		ChangePListStyle(oPList, "Exploded Parts List")
	End If
End Sub

Sub ChangePListStyle(oPL As PartsList, oNewStyleName As String)
	Dim oDrawDoc As DrawingDocument = oPL.Parent.Parent
	Try
		oPL.Style = oDrawDoc.StylesManager.PartsListStyles.Item(oNewStyleName)
	Catch
		MsgBox("Failed to change Parts List Style to new Style.",,"")
	End Try
 End Sub

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 7

SharkDesign
Mentor
Mentor
Accepted solution

That works with a slight change, you put part instead of presentation. Was that a test? 😀

Thank you so much, that's exactly what I want!

 

Dim oDrawDoc As DrawingDocument = ThisDrawing.Document
For Each oSheet As Sheet In oDrawDoc.Sheets
	If oSheet.PartsLists.Count > 0
		Dim oPartsList As PartsList = oSheet.PartsLists(1)
		Dim oView As DrawingView = oSheet.DrawingViews(1)
		Select Case oView.ReferencedDocumentDescriptor.ReferencedDocument.DocumentType
			Case DocumentTypeEnum.kAssemblyDocumentObject
				'The sheets first view is of an assembly, maybe you want to do something here?
			Case DocumentTypeEnum.kPresentationDocumentObject
				'The sheets first view is a part, set style of partslist
				oPartsList.Style = oDrawDoc.StylesManager.PartsListStyles.Item("Exploded Parts List")
		End Select
	Else
		Logger.Info("No partslist in " & oSheet.Name)
	End If
Next

 

  Inventor Certified Professional
Message 6 of 7

SharkDesign
Mentor
Mentor

@WCrihfield yours works too, but the first one is shorter 😋

 

Thank you very much!

  Inventor Certified Professional
0 Likes
Message 7 of 7

JhoelForshav
Mentor
Mentor

@SharkDesign 

I'm glad I could help!

Hahah sure, lets say it was a test and not just me making a mistake 😉

0 Likes