Automatic sheet generation for .idw

Automatic sheet generation for .idw

BK-MTFNE
Advocate Advocate
861 Views
10 Replies
Message 1 of 11

Automatic sheet generation for .idw

BK-MTFNE
Advocate
Advocate

Ok so I cobbled this iLogic together for my needs but it is failing at line 25 with the following error code and I cant find anything to make it work. any help would be appreciated.

 

BKMTFNE_0-1689893043920.png

 

 

Dim oDoc As DrawingDocument
oDoc = ThisApplication.ActiveDocument

' Check if the active document is a drawing referencing a model
If oDoc.DocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
    MessageBox.Show("This rule is intended for drawings only.", "Error")
    Return
End If

' Get the referenced model document
Dim modelDoc = ThisDrawing.ModelDocument


' Check if the referenced document is a part or assembly
If modelDoc.DocumentType <> DocumentTypeEnum.kPartDocumentObject AndAlso
    modelDoc.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
    MessageBox.Show("This rule is intended for drawings referencing parts or assemblies only.", "Error")
    Return
End If

' Continue with the rest of the code...


' Get the active assembly drawing document
Dim asmDoc As AssemblyDocument = oDoc

' Find the sheet format named "24 x 36"
Dim sheetFormatName As String = "24 x 36"
Dim sheetFormat As SheetFormat = Nothing
For Each sf As sheetFormat In oDoc.SheetFormats
    If sf.Name = sheetFormatName Then
        sheetFormat = sf
        Exit For
    End If
Next

' Check if the sheet format was found
If sheetFormat Is Nothing Then
    MessageBox.Show("Sheet format '24 x 36' not found.", "Error")
    Return
End If

' Loop through the parts in the assembly
For Each partOccurrence As ComponentOccurrence In asmDoc.ComponentDefinition.Occurrences
    ' Get the part document for each component occurrence
    If partOccurrence.DefinitionDocumentType = DocumentTypeEnum.kPartDocumentObject Then
        Dim partDoc As PartDocument = partOccurrence.Definition.Document

        ' Create a new sheet using the specified sheet format
        Dim newSheet As Sheet = oDoc.Sheets.AddUsingSheetFormat(sheetFormat)

        ' Set the sheet name to the part name
        newSheet.Name = partDoc.DisplayName

        ' Activate the new sheet
        newSheet.Activate()

        ' Add the part view to the new sheet
        Dim partView As DrawingView = oDoc.ActiveSheet.DrawingViews.AddBaseView(partOccurrence, Nothing, Nothing, Nothing, Nothing)
    End If
Next

' Activate the first sheet
oDoc.Sheets.Item(1).Activate()

 

0 Likes
Accepted solutions (2)
862 Views
10 Replies
Replies (10)
Message 2 of 11

A.Acheson
Mentor
Mentor

Hi @BK-MTFNE 

 

So the error is that you are trying to set the Assembly Document Object (asmDoc)  = to the Drawing Document Object (oDoc) 

This is wrong. What you actually need is the model document (modeldoc) which you allready have so you could remove this line.

 

In fact you may need to check if the model document is an assembly before you try use it in an assembly application. Later on I see you are looping through assembly occurrences and creating views using the occurrence. This wouod beed to be a document object. I would suggest you do this by ReferencedDocuments as this will give you one document reference immediately.  It has the added benefit of being more efficient instead of trying to dig down through the occurrence definition and through all of the multiple occurrences.

 

Here is a sample for ReferenceDoument Method

Sub Main

Dim assyDoc As AssemblyDocument = ThisApplication.ActiveDocument

' Iterate through all Referenced Documents in the assembly.
For Each refDoc As Document In assyDoc.AllReferencedDocuments
   
	Logger.Info("Display Name Before Check: " & refDoc.DisplayName)
    
	' Check if the document is a sheet metal part.
    If refDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then 
			
	   partDoc = TryCast(refDoc,PartDocument)
		Logger.Info("Display Name:" & partDoc.DisplayName)
    End If
Next
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 3 of 11

BK-MTFNE
Advocate
Advocate

Thank you @A.Acheson for getting back to me, I have attempted to make the changes you referenced however I am not nearly proficent enough in iLogic to make it work. I am thinking I will need to skip this iLogic and come back to it at a later date when I have become more proficient in coding. Again thank you for your help.

0 Likes
Message 4 of 11

A.Acheson
Mentor
Mentor

Here is the method you were trying with the reference documents implemented. I have not tested this so you may find errors. It will assume your first view is an assembly.

 

And here is the API help for drawings views. There is alot to learn here so I would suggest to review the samples in there too. Knowing the options to change is the key to getting the views customized.

 

Dim doc As DrawingDocument = ThisApplication.ActiveDocument

' Check if the active document is a drawing referencing a model
If doc.DocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
    MessageBox.Show("This rule is intended for drawings only.", "Error")
    Return
End If

' Find the sheet format named "24 x 36"
Dim sheetFormatName As String = "24 x 36"
Dim sheetFormat As SheetFormat = Nothing
For Each sf As sheetFormat In oDoc.SheetFormats
    If sf.Name = sheetFormatName Then
        sheetFormat = sf
        Exit For
    End If
Next

' Check if the sheet format was found
If sheetFormat Is Nothing Then
    MessageBox.Show("Sheet format '24 x 36' not found.", "Error")
    Return
End If

' Create a new NameValueMap object
Dim baseViewOptions As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap

baseViewOptions.Add("DesignViewAssociative", True)

' Get the referenced model document
Dim modelDoc As Document= ThisDrawing.ModelDocument

' Iterate through all Referenced Documents in the assembly.
For Each refDoc As Document In modelDoc.AllReferencedDocuments
   
	Logger.Info("Display Name Before Check: " & refDoc.DisplayName)
    
	' Check if the document is a sheet metal part.
    If refDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then 
			
	   partDoc = TryCast(refDoc,PartDocument)

		Logger.Info("Display Name:" & partDoc.DisplayName)

       ' Create a new sheet using the specified sheet format
        Dim newSheet As Sheet = doc.Sheets.AddUsingSheetFormat(sheetFormat)

        ' Set the sheet name to the part name
        newSheet.Name = partDoc.DisplayName

        ' Activate the new sheet
        newSheet.Activate()

       ' Create the placement point object.
       Dim oPoint As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(25, 25)

       ' Create a base view.
       Dim baseView As DrawingView = doc.ActiveSheet.DrawingViews.AddBaseView(oModel,oPoint,2,kIsoTopLeftViewOrientation,kHiddenLineRemovedDrawingViewStyle,,baseViewOptions)
    End If
Next
' Activate the first sheet
oDoc.Sheets.Item(1).Activate()

 

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

BK-MTFNE
Advocate
Advocate

Oh wow, I see where i was going wrong with the referenced documents. I fixed a couple errors that came up but I cant seem to figure out what is wrong on line 47

 

' Create a new sheet using the specified sheet format
        Dim newSheet As Sheet = oDoc.Sheets.AddUsingSheetFormat(sheetFormat)

If it already found the sheet format earlier in the code I'm not sure why its saying  the parameter is incorrect. Is the sheet format name "24 x 36" supposed to be specifically called out?

0 Likes
Message 6 of 11

A.Acheson
Mentor
Mentor
Accepted solution

Hi @BK-MTFNE 

If you are using my code I changed around the variable name for the drawing document from oDoc to doc. I also spotted another error in the model document variable name. And also you can create views directly from the sheet format so no need for additional view creation. 

See working code below.

 

Dim doc As DrawingDocument = ThisApplication.ActiveDocument

' Check if the active document is a drawing referencing a model
If doc.DocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
    MessageBox.Show("This rule is intended for drawings only.", "Error")
    Return
End If

' Find the sheet format named "24 x 36"
Dim sheetFormatName As String = "24 x 36"
Dim sheetFormat As SheetFormat = Nothing
For Each sf As sheetFormat In doc.SheetFormats
    If sf.Name = sheetFormatName Then
        sheetFormat = sf
        Exit For
    End If
Next

' Check if the sheet format was found
If sheetFormat Is Nothing Then
    MessageBox.Show("Sheet format '24 x 36' not found.", "Error")
    Return
End If

' Get the referenced model document
Dim modelDoc As Document= ThisDrawing.ModelDocument

' Iterate through all Referenced Documents in the assembly.
For Each refDoc As Document In modelDoc.AllReferencedDocuments
   
	Logger.Info("Display Name Before Check: " & refDoc.DisplayName)
    
	' Check if the document is a sheet metal part.
    If refDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then 
			
	   partDoc = TryCast(refDoc,PartDocument)

		Logger.Info("Display Name:" & partDoc.DisplayName)
	
       ' Create a new sheet using the specified sheet format
        Dim newSheet As Sheet = doc.Sheets.AddUsingSheetFormat(sheetFormat,refDoc,partDoc.DisplayName)

    End If
Next

' Activate the first sheet
doc.Sheets.Item(1).Activate()

 

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

BK-MTFNE
Advocate
Advocate

@A.Acheson You are awesome, I cant thank you enough. this has been stumping me for awhile. It works perfect.

0 Likes
Message 8 of 11

A.Acheson
Mentor
Mentor

No problem at all. My first go at sheetformats so it will go in my list for implementation soon as it had a nice outcome on the drawings views.  

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

BK-MTFNE
Advocate
Advocate

@A.Acheson  This has been working flawlessly but i recently had an issue come up with a model where there are sub-assemblies. I would like it to also create the sheets for the sub-assemblies. I attempted to add the ability to do this a few ways but I keep crashing the iLogic. Is there something I'm missing that prevents this or am I just completely messing up.

 

For your information once I came across this line

DocumentTypeEnum.kPartDocumentObject

 I was lost cause I can't find decent documentation on this type of iLogic.

0 Likes
Message 10 of 11

A.Acheson
Mentor
Mentor
Accepted solution

If you want assemblies and parts then just remove the filter lines for parts only and use the more generic document class.

 

The new for statement

' Iterate through all Referenced Documents in the assembly.
For Each refDoc As Document In modelDoc.AllReferencedDocuments
   
	'Logger.Info("Display Name Before Check: " & refDoc.DisplayName)
   	
       ' Create a new sheet using the specified sheet format
        Dim newSheet As Sheet = doc.Sheets.AddUsingSheetFormat(sheetFormat,refDoc,refDoc.DisplayName)

Next

 

The DocumentTypeEnum help page is here 

And under document you will find its a property here

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

BK-MTFNE
Advocate
Advocate

I was trying to add a "search" for  assemblies, I didnt think that the assemblies were filtered. It works perfectly. Also thank you for the links, I will definitely be studying them.