Add the title Block in the sheets

Add the title Block in the sheets

richie_tony007
Participant Participant
139 Views
1 Reply
Message 1 of 2

Add the title Block in the sheets

richie_tony007
Participant
Participant

Hi All,

 

I was trying to add the title block in my sheets using this code 

 

 
					' Get the specific title block definition and apply it
					Dim oTitleBlockDef As TitleBlockDefinition = oDrawingDoc.TitleBlockDefinitions.Item("MKT_A3_STANDARD_INSTALL")
					
					oNewSheet.AddTitleBlock(oTitleBlockDef)

 

But i end up getting error as parameter is incorrect 

 

Can anyone help please

 

This is the full code 

 

Sub Main()
' Get the active document (must be an assembly)
Dim modelReference As Document = ThisApplication.ActiveDocument

' Check if the active document is an assembly
If modelReference.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
	MessageBox.Show("The active document must be an assembly.")
	Exit Sub
End If

' Create a new drawing
Dim oDrawingDoc As DrawingDocument = CreateSheet()

' Check if the drawing was created successfully
If oDrawingDoc Is Nothing Then
	MessageBox.Show("Failed to create the drawing. Rule execution aborted.")
	Exit Sub
End If

' Pass the model and drawing to create views and the parts list
ShowAssemblyViews(modelReference, oDrawingDoc)
End Sub

Function CreateSheet() As DrawingDocument
	' Specify the path to the template file
	Dim templatePath As String = "C:\_VaultPro\Templates\MKT DESIGN TEMPLATE.dwg"

	' Check if the template file exists
	If Not System.IO.File.Exists(templatePath) Then
		MessageBox.Show("Error: Template file '" & templatePath & "' not found. Ensure the path is correct and the file is accessible.")
		Return Nothing
	End If

	' Try to create a new drawing based on the template
	Try
		Dim oDoc As DrawingDocument = ThisApplication.Documents.Add(DocumentTypeEnum.kDrawingDocumentObject, templatePath)

		' Activate the first sheet
		Dim oSheet As Sheet = oDoc.Sheets.Item(1)

		' Notify about successful drawing creation
		'MessageBox.Show("Drawing successfully created from the template!")

		' Return the created drawing
		Return oDoc
	Catch ex As Exception
		MessageBox.Show("Error creating drawing: " & ex.Message)
		Return Nothing
	End Try
End Function

Sub ShowAssemblyViews(modelReference As Document, oDrawingDoc As DrawingDocument)
	' Check if the document is an assembly
	If modelReference.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
		Dim oAsmDoc As AssemblyDocument = modelReference

		' Get the first sheet (to place the assembly iso view)
		Dim oSheet As Sheet = oDrawingDoc.Sheets.Item(1)

		' Create the isometric view of the full assembly
		CreateAssemblyIsoView(oAsmDoc, oSheet)

		' Create parts list on the second sheet
		CreatePartsList(oAsmDoc, oDrawingDoc)

		' Create unique sheets for individual parts
		ShowPanelParts(oAsmDoc, oDrawingDoc)
	Else
		MessageBox.Show("The provided document is not an assembly.")
	End If
End Sub

Sub CreateAssemblyIsoView(oAsmDoc As AssemblyDocument, oSheet As Sheet)
	' Set the scale for the assembly isometric view
	Dim scale As Double = 1 / 25

	' Define the view location on the sheet
	Dim isoViewHorizontal As Double = (oSheet.Width * 0.5)
	Dim isoViewVertical As Double = (oSheet.Height * 0.5)

	' Create the isometric view
	Dim oIsoViewPoint As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(isoViewHorizontal, isoViewVertical)
	Dim isoViewOrientation As ViewOrientationTypeEnum = kIsoTopRightViewOrientation
	Dim isoViewStyle As DrawingViewStyleEnum = kShadedDrawingViewStyle

	Try
		Dim oIsoView As DrawingView = oSheet.DrawingViews.AddBaseView(oAsmDoc, oIsoViewPoint, scale, isoViewOrientation, isoViewStyle)
		oIsoView.Name = "ISOMETRIC VIEW"
	Catch ex As Exception
		MessageBox.Show("Error creating assembly isometric view: " & ex.Message)
	End Try
End Sub

Sub CreatePartsList(oAsmDoc As AssemblyDocument, oDrawingDoc As DrawingDocument)
	Try
		' Get the first drawing view on the first sheet (this should be the full assembly view)
		Dim oSheet As Sheet = oDrawingDoc.Sheets.Item(1)
		Dim oDrawingView As DrawingView = oSheet.DrawingViews.Item(1)

		' Define position for the parts list (on the second sheet)
		Dim oNewSheet As Sheet = oDrawingDoc.Sheets.Add(DrawingSheetSizeEnum.kA3DrawingSheetSize)
		oNewSheet.Name = "Parts List"

		' Get the specific border definition
		Dim oBorderDef As BorderDefinition = oDrawingDoc.BorderDefinitions.Item("A3_LANDSCAPE") ' Change the name if needed

		' Apply the border to the new sheet
		oNewSheet.AddBorder(oBorderDef)

		Dim oPosition As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(40.3, 27.7)

		' Create the parts list (BOM) using the assembly drawing view
		Dim oPartsList As PartsList = oNewSheet.PartsLists.Add(oDrawingView, oPosition)

		' Auto arrange and renumber parts
		oPartsList.Renumber()

		' Set the parts list style (optional)
		oPartsList.Style = oDrawingDoc.StylesManager.PartsListStyles.Item("Default")

		MessageBox.Show("Parts list successfully created on the second sheet.")
	Catch ex As Exception
		'        MessageBox.Show("Error creating parts list: " & ex.Message)
	End Try
End Sub

Sub ShowPanelParts(oAsmDoc As AssemblyDocument, oDrawingDoc As DrawingDocument)
	' Get the first sheet (to place individual part views)
	Dim oSheet As Sheet = oDrawingDoc.Sheets.Item(1)

	' Create a collection for unique part names
	Dim uniqueParts As New Collection

	' Loop through all components in the assembly
	For Each oComp As ComponentOccurrence In oAsmDoc.ComponentDefinition.Occurrences
		If oComp.Suppressed = False Then
			' Check if the component name contains "PANEL"
			If InStr(oComp.Name, "PANEL") > 0 Then
				Dim partName As String = oComp.Name.ToUpper()
				partName = Replace(partName, " LEFT", "")
				partName = Replace(partName, " RIGHT", "")

				' Check if the part is already in the collection
				Dim isDuplicate As Boolean = False
				For Each uniquePart As String In uniqueParts
					If uniquePart = partName Then
						isDuplicate = True
						Exit For
					End If
				Next

				' If the part is unique, add it and create a new sheet
				If Not isDuplicate Then
					uniqueParts.Add(partName)

					' Create a new sheet for this part
					Dim oNewSheet As Sheet = oDrawingDoc.Sheets.Add(DrawingSheetSizeEnum.kA3DrawingSheetSize)

					oNewSheet.Name = partName

					' Get the specific border definition
					Dim oBorderDef As BorderDefinition = oDrawingDoc.BorderDefinitions.Item("A3_LANDSCAPE") ' Change the name if needed

					' Apply the border to the new sheet
					oNewSheet.AddBorder(oBorderDef)

					' Get the specific title block definition and apply it
					Dim oTitleBlockDef As TitleBlockDefinition = oDrawingDoc.TitleBlockDefinitions.Item("MKT_A3_STANDARD_INSTALL")
					
					oNewSheet.AddTitleBlock(oTitleBlockDef)

					' Create views for the part
					CreateViews(oComp, oNewSheet)
				End If
			End If
		End If
	Next
End Sub

Sub CreateViews(oComp As ComponentOccurrence, oSheet As Sheet)
	' Set the scale for the views of individual parts
	Dim scale As Double = 1 / 12

	' Define the view locations on the sheet
	Dim sheetCenterVertical As Double = (oSheet.Height / 1.75)
	Dim isoViewHorizontal As Double = (oSheet.Width * 0.25)
	Dim rightViewHorizontal As Double = (oSheet.Width * 0.75)

	' Create points for view placement
	Dim oIsoViewPoint As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(isoViewHorizontal, sheetCenterVertical)
	Dim oRightViewPoint As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(rightViewHorizontal, sheetCenterVertical)

	' Define view orientations
	Dim isoViewOrientation As ViewOrientationTypeEnum = kIsoTopRightViewOrientation
	Dim rightViewOrientation As ViewOrientationTypeEnum = kRightViewOrientation

	' Define view styles
	Dim isoViewStyle As DrawingViewStyleEnum = kShadedDrawingViewStyle
	Dim rightViewStyle As DrawingViewStyleEnum = kHiddenLineRemovedDrawingViewStyle

	' Create views for the component
	Try
		Dim oIsoView As DrawingView = oSheet.DrawingViews.AddBaseView(oComp.Definition.Document, oIsoViewPoint, scale, isoViewOrientation, isoViewStyle)
		oIsoView.Name = " ISOMETRIC VIEW"

		Dim oRightView As DrawingView = oSheet.DrawingViews.AddBaseView(oComp.Definition.Document, oRightViewPoint, scale, rightViewOrientation, rightViewStyle)
		oRightView.Name = " RIGHT VIEW"
	Catch ex As Exception
		'        MessageBox.Show("Error creating views for part '" & oComp.Name & "': " & ex.Message)
	End Try
End Sub

0 Likes
140 Views
1 Reply
Reply (1)
Message 2 of 2

a.brusamolino
Enthusiast
Enthusiast

Hi @richie_tony007  I tested your code and it works correctly for me.
The only thing to check is that the title block name you want to insert is correct and is present in your idw template.
When you open a new drawing document, do you see it in your list of title blocks?

0 Likes