Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

Adding Part List to each view present in the Drawing sheet and skip if the part list already Exist

Anonymous

Adding Part List to each view present in the Drawing sheet and skip if the part list already Exist

Anonymous
Not applicable

Hello Everyone,

 

I am trying to write a code where I want to put a part list for each individual part that is available on the drawing sheet. But I don't know how to try and catch it part. 

 

The following code is written such that it allows to select the view and also adds the parts list for that part. 

 

But the problem arises when I want the section cut for the projected views as well and so it adds part lists for each projected view, Despite I only need one part list for that part. Please help!

 

Here's the code:

 

Sub Main()  
Start:
	Dim oDrawDoc As DrawingDocument
    oDrawDoc = ThisApplication.ActiveDocument

    ' Select a drawing view.
    Dim oView As DrawingView
    oView = ThisApplication.CommandManager.Pick(kDrawingViewFilter, "Select a drawing view.")
    
	
	
	
    'Set a reference to the active sheet.
    Dim oSheet As Sheet
    oSheet = oView.Parent
	
	
	
	Dim dXval As Double 
	dXval = oView.Left
	
	Dim dYval As Double
    dYval = oView.Center.Y
   
	
	Dim dLeftXval As Double
	dLeftXval = dXval - 3

    'add height to y value
    Dim dUpperYval As Double
    dUpperYval = dYval + (0.5 * oView.Height) + 1
   
    'subract  height To y value
    Dim dLowerYval As Double
    dLowerYval = dYval - (0.5 * oView.Height) - 1
   
    'Dim oPoint1 As Point2d
    'oPoint1 = ThisApplication.TransientGeometry.CreatePoint2d(oView.Center.X,dUpperYval)
   
    'Dim oPoint2 As Point2d
    'oPoint2 = ThisApplication.TransientGeometry.CreatePoint2d(oView.Center.X,dLowerYval)
   
   
    Dim oPoint1 As Point2d
    oPoint1 = ThisApplication.TransientGeometry.CreatePoint2d(dLeftXval,dUpperYval)
   
    Dim oPoint2 As Point2d
    oPoint2 = ThisApplication.TransientGeometry.CreatePoint2d(dLeftXval,dLowerYval)
   
 
   
    'create a new sketch For the section
    Dim oDrawingSketch As DrawingSketch = oView.Sketches.Add
	
	
	Dim oSketchPoint1 As Point2d
    oSketchPoint1 = oDrawingSketch.SheetToSketchSpace(oPoint1)
   

    Dim oSketchPoint2 As Point2d
    oSketchPoint2 = oDrawingSketch.SheetToSketchSpace(oPoint2)
       
	
	oDrawingSketch.Edit
    Dim oSketchLine As SketchLine
    oSketchLine = oDrawingSketch.SketchLines.AddByTwoPoints(oSketchPoint1,oSketchPoint2)
    oDrawingSketch.ExitEdit
   

    'set the location for the view
    Dim olocal As Point2d
    olocal = ThisApplication.TransientGeometry.CreatePoint2d(6,oView.Center.Y)
   
    Dim oView2 As SectionDrawingView
    oView2 = oSheet.DrawingViews.AddSectionView(oView, oDrawingSketch, olocal,DrawingViewStyleEnum.kFromBaseDrawingViewStyle, Nothing, , , False, True)
   
    oDrawingSketch.Visible = True
	
	'AutoGenerate PartList
	
	Dim oPlacementPoint As Point2d

	oPlacementPoint = ThisApplication.TransientGeometry.CreatePoint2d(dLeftXval+22, dUpperYval+3)

     
    iLogicVb.UpdateWhenDone = True
	
	
	Dim oPartslist As PartsList
	'Dim C As Double
	Try 
	'try to get the parts list
	
		oPartslist = oSheet.PartsLists.Item(1)
	
	Catch
		
		
	'catch error when no parts list is found
	'and then create one
	oPartslist = oSheet.PartsLists.Add(oView, oPlacementPoint)
	
    End Try
	
	
	
	'Selection if you want to continue with section cut
	
    Dim oChoice As String
    oChoice = MsgBox(" Do you want to create another section cut?", vbQuestion + vbYesNo + vbDefaultButton2, "Section Cut")
    If oChoice = vbYes Then
        GoTo Start
    Else
         
    End If
	
End Sub

0 Likes
Reply
Accepted solutions (2)
928 Views
6 Replies
Replies (6)

JelteDeJong
Mentor
Mentor
Accepted solution

does this work for you?

[iLogic]

Start:
Dim oDrawDoc As DrawingDocument = ThisApplication.ActiveDocument
' Select a drawing view.
Dim oView As DrawingView = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kDrawingViewFilter, "Select a drawing view.")

'Set a reference to the active sheet.
Dim oSheet As Sheet = oView.Parent

Dim dXval As Double = oView.Left
Dim dYval As Double = oView.Center.Y


Dim dLeftXval As Double = dXval - 3

'add height to y value
Dim dUpperYval As Double = dYval + (0.5 * oView.Height) + 1

'subract  height To y value
Dim dLowerYval As Double = dYval - (0.5 * oView.Height) - 1


Dim oPoint1 As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(dLeftXval, dUpperYval)
Dim oPoint2 As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(dLeftXval, dLowerYval)

'create a new sketch For the section
Dim oDrawingSketch As DrawingSketch = oView.Sketches.Add

Dim oSketchPoint1 As Point2d = oDrawingSketch.SheetToSketchSpace(oPoint1)
Dim oSketchPoint2 As Point2d = oDrawingSketch.SheetToSketchSpace(oPoint2)


oDrawingSketch.Edit()
Dim oSketchLine As SketchLine = oDrawingSketch.SketchLines.AddByTwoPoints(oSketchPoint1, oSketchPoint2)
oDrawingSketch.ExitEdit()


'set the location for the view
Dim olocal As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(6, oView.Center.Y)
Dim oView2 As SectionDrawingView = oSheet.DrawingViews.AddSectionView(oView, oDrawingSketch, olocal, DrawingViewStyleEnum.kFromBaseDrawingViewStyle, Nothing, , , False, True)

oDrawingSketch.Visible = True

'AutoGenerate PartList

Dim oPlacementPoint As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(dLeftXval + 22, dUpperYval + 3)

' check if we can find a partlist that belongs to the seelcted view
Dim viewPartName = oView.ReferencedFile.FullFileName
Dim aPartListIsFound = False
For Each pl As PartsList In oSheet.PartsLists
    Dim plFileName = pl.ParentView.ReferencedFile.FullFileName
    If (viewPartName.Equals(plFileName)) Then
        aPartListIsFound = True
    End If
Next
' If none partslist was found then create it.
If (aPartListIsFound = False) Then
    oSheet.PartsLists.Add(oView, oPlacementPoint)
End If


'Selection if you want to continue with section cut

Dim oChoice As String
oChoice = MsgBox(" Do you want to create another section cut?", vbQuestion + vbYesNo + vbDefaultButton2, "Section Cut")
If oChoice = vbYes Then
    GoTo Start
Else

End If

 

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

J-Camper
Advisor
Advisor
Accepted solution

I condensed the code a little and change the Try/Catch into a loop:

Sub Main() 
	If ThisApplication.ActiveDocumentType <> kDrawingDocumentObject Then MessageBox.Show("This rule is designed to only work in drawing documents.", "Wrong Document Type") : Exit Sub
Start:
	Dim oDrawDoc As DrawingDocument = ThisApplication.ActiveDocument

    ' Select a drawing view.
    Dim oView As DrawingView = ThisApplication.CommandManager.Pick(kDrawingViewFilter, "Select a drawing view.")

    'Set a reference to the active sheet.
    Dim oSheet As Sheet = oView.Parent
		
	Dim dXval As Double  = oView.Left
	
	Dim dYval As Double = oView.Center.Y
   
	Dim dLeftXval As Double = dXval - 3

    'add height to y value
    Dim dUpperYval As Double = dYval + (0.5 * oView.Height) + 1
   
    'subract  height To y value
    Dim dLowerYval As Double = dYval - (0.5 * oView.Height) - 1
   
    Dim oPoint1 As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(dLeftXval,dUpperYval)
   
    Dim oPoint2 As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(dLeftXval,dLowerYval)
      
    'create a new sketch For the section
    Dim oDrawingSketch As DrawingSketch = oView.Sketches.Add
		
	Dim oSketchPoint1 As Point2d = oDrawingSketch.SheetToSketchSpace(oPoint1)
   
    Dim oSketchPoint2 As Point2d = oDrawingSketch.SheetToSketchSpace(oPoint2)
       
	oDrawingSketch.Edit
    Dim oSketchLine As SketchLine = oDrawingSketch.SketchLines.AddByTwoPoints(oSketchPoint1,oSketchPoint2)
    oDrawingSketch.ExitEdit
   
    'set the location for the view
    Dim olocal As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(6,oView.Center.Y)
   
    Dim oView2 As SectionDrawingView = oSheet.DrawingViews.AddSectionView(oView, oDrawingSketch, olocal,DrawingViewStyleEnum.kFromBaseDrawingViewStyle, Nothing, , , False, True)
   
    oDrawingSketch.Visible = True
	
	'AutoGenerate PartList
	
	Dim oPlacementPoint As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(dLeftXval+22, dUpperYval+3)
     
    iLogicVb.UpdateWhenDone = True
	
	For Each pl As PartsList In oSheet.PartsLists
		If pl.ReferencedDocumentDescriptor.FullDocumentName = oView.ReferencedDocumentDescriptor.FullDocumentName
			GoTo Done 'Parts list Exists so exiting loop to avoid making again
		End If
		
	Next
	'If exiting loop normally, then Partslist was not found so we will make it
	Dim oPartslist As PartsList = oSheet.PartsLists.Add(oView, oPlacementPoint)
Done :
	'Selection if you want to continue with section cut
	Dim oChoice As String = MsgBox(" Do you want to create another section cut?", vbQuestion + vbYesNo + vbDefaultButton2, "Section Cut")
    If oChoice = vbYes Then GoTo Start
	
End Sub

Let me know if you're still having issues.

Anonymous
Not applicable

Thank you very much. It worked perfectly! 

0 Likes

Anonymous
Not applicable

Thank you very much. This one also worked perfectly. Really appreciate it!

Both the ways gave me the insight the use of ReferencedDocumentDescriptor and how the same thing is achieved in two different ways.

 

Got to learn something new! :slightly_smiling_face:

0 Likes

aiyer8WKRH
Explorer
Explorer

Hello, 

 

This rule works awesome for section cuts and part lists. However, it creates the section cut on the left side. Is there a way to create them on the right side as well for custom representations. Any help is appreciated. 

@J-Camper @Anonymous @JelteDeJong 

0 Likes

J-Camper
Advisor
Advisor

@aiyer8WKRH ,

 

You would need to change the X value for the section line to be on the right side of the view and reverse the direction of the section cut when making it.  I’m away from my computer so I can’t rewrite the code right now, but it shouldn’t be too difficult.

 

Section view creation method 

 

and the section line x value can be set to the right side by setting it equal to drawingview.left + drawingview.width + an offset.

 

Let me know if you need help actually rewriting it

0 Likes