Generate drawing with 3 views from Assembly and if it is a Sheetmetal with a flat pattern then show that too as a 4th view.

Generate drawing with 3 views from Assembly and if it is a Sheetmetal with a flat pattern then show that too as a 4th view.

Covello_O
Enthusiast Enthusiast
617 Views
7 Replies
Message 1 of 8

Generate drawing with 3 views from Assembly and if it is a Sheetmetal with a flat pattern then show that too as a 4th view.

Covello_O
Enthusiast
Enthusiast

 

 

I tried this code but unfortunately an error occurred

' Declare document variables
Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument
If oAsmDoc.DocumentType <> kAssemblyDocumentObject Then
    MessageBox.Show("This script is only valid for assembly documents.", "Invalid Document Type")
    Return
End If

' Set the template path and create a new drawing document
Dim oTemplatePath As String = ThisApplication.FileOptions.TemplatesPath
Dim oDrawingDoc As DrawingDocument = ThisApplication.Documents.Add( _
DocumentTypeEnum.kDrawingDocumentObject, oTemplatePath & "\abb_a3.idw", True)

' Add an assembly view to the drawing
Dim oSheet As Sheet = oDrawingDoc.Sheets(1)
Dim oBaseView As DrawingView
Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
Dim oBaseViewPoint As Point2d = oTG.CreatePoint2d(2, 2) ' Position of the base view on the sheet
oBaseView = oSheet.DrawingViews.AddBaseView(
    oAsmDoc, 
    oBaseViewPoint, 
    0.1, 
    ViewOrientationTypeEnum.kDefaultViewOrientation, 
    DrawingViewStyleEnum.kFromBaseDrawingViewStyle)

' Loop through all components in the assembly to find sheet metal components
For Each oCompOcc As ComponentOccurrence In oAsmDoc.ComponentDefinition.Occurrences
    If TypeOf oCompOcc.Definition Is SheetMetalComponentDefinition Then
        Dim oSMDef As SheetMetalComponentDefinition = oCompOcc.Definition
        ' Check if the sheet metal component has a flat pattern
        If oSMDef.HasFlatPattern Then
            ' Define the placement point for the flat pattern view
            Dim oFlatPatternPoint As Point2d = oTG.CreatePoint2d(2, 12) ' Adjust position as necessary
            
            ' Define flat pattern view options inline
            Dim oOptions As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
            oOptions.Add("SheetMetalFoldedModel", False) ' Specify flat pattern
            
            ' Add the flat pattern view to the sheet
            Dim oFlatPatternView As DrawingView = oSheet.DrawingViews.AddBaseView(
                oCompOcc,
                oFlatPatternPoint,
                0.25, 
                ViewOrientationTypeEnum.kDefaultViewOrientation, 
                DrawingViewStyleEnum.kFromBaseDrawingViewStyle,
                Nothing, Nothing, oOptions)
        End If
    End If
Next

' Update the drawing to reflect all changes
oDrawingDoc.Update()

 

Hello,

At work I must make drawings for production. If an Assembly or Subassy has a Sheetmetal part, then they need a Flat pattern View too.

I would like to generate a drawing from an assembly Model with 4 views. Front, right and top and it should check if an occurrence is a Sheetmetal with a flat pattern then show the flat pattern too, as a 4th view.

I would use it only in assembly where I know there is only one Sheetmetal part among other buy parts i.e. pem’s, cable support etc.

I found a lot of code that generates views with flatpattern but works only for parts.

Can someone please help me? Any help would be very appreciated!

 

----------------------------------------

 It should look something like this:

 

Screenshot 2024-05-04 130757.png

0 Likes
Accepted solutions (1)
618 Views
7 Replies
Replies (7)
Message 2 of 8

A.Acheson
Mentor
Mentor

Hi @Covello_O 

Edit:

Can you verify where the error occurs? When debugging place a message in each line of code and when the error appears and message dissapears this is your problem line. Post the more info tab of the error message. 

 

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 8

Covello_O
Enthusiast
Enthusiast

Sorrry, may you make an example on how to place an error message in each line?

here  is the more info tab of the error message
Screenshot 2024-05-05 190316.png

0 Likes
Message 4 of 8

A.Acheson
Mentor
Mentor

Paste a line like below which is just a visual indicator that your code has advanced to this line of code without error. 

 

 

MessageBox.Show("Code ok to this point")

 

Can you find out which line the error message appears on? Keep moving the message box line by line until it dissapears and the error message appears. I have an idea of what the error message is which is it is missing a (string value) and where so can you find the same line by debugging? So instead of me telling you which line is causing the issue you can debug first and find the line and then read the error message and see what you think is missing and does it make sense. 

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

BM_Ashraf
Advocate
Advocate

Hi,

Just replace this

BM_Ashraf_0-1714980154917.png

 


With  this

oBaseView = oSheet.DrawingViews.AddBaseView(oAsmDoc, oBaseViewPoint, 0.1,
ViewOrientationTypeEnum.kDefaultViewOrientation, DrawingViewStyleEnum.kHiddenLineRemovedDrawingViewStyle)

and if you are looking for a ready to use and reliable solution please check
https://bluemech.de/product/inventor_adc_automatic_drawing_creator/

Here is a quick review of how it works.
https://www.youtube.com/watch?v=XWoRR8oPh60

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.

Blue Mech

Add-ins for Inventor!

0 Likes
Message 6 of 8

Covello_O
Enthusiast
Enthusiast

Hi

@A.Acheson Thanks for the simple tip to debugging with error messages.

I already saw the ilogic error message that the error must be in line 18 but I couldn’t figure out what was wrong.

Thanks to @BM_Ashraf  to show me the culprit.  I've come a step further.

But now I get stuck in line 47, though.

I think it is the oCompOcc at line 48 that makes problems?

it throws this error message now

Screenshot 2024-05-06 230425.png

 

The code looks like this now

' Declare document variables
Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument
If oAsmDoc.DocumentType <> kAssemblyDocumentObject Then
    MessageBox.Show("This script is only valid for assembly documents.", "Invalid Document Type")
    Return
End If

' Set the template path and create a new drawing document
Dim oTemplatePath As String = ThisApplication.FileOptions.TemplatesPath

Dim oDrawingDoc As DrawingDocument = ThisApplication.Documents.Add( _
	DocumentTypeEnum.kDrawingDocumentObject, oTemplatePath & "\abb_a3.idw", True)

' Add an assembly view to the drawing
Dim oSheet As Sheet = oDrawingDoc.Sheets(1)
Dim oBaseView As DrawingView

'Dim oViews As DrawingViews = oAsmDoc.ActiveSheet.DrawingViews
Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
Try
	Dim oBaseViewPoint As Point2d = oTG.CreatePoint2d(5, 15) ' Position of the base view on the sheet
	oBaseView = oSheet.DrawingViews.AddBaseView(
				oAsmDoc, 
				oBaseViewPoint,
				0.5,
				ViewOrientationTypeEnum.kDefaultViewOrientation, 
				DrawingViewStyleEnum.kHiddenLineRemovedDrawingViewStyle)
				
Catch ex As Exception
	MessageBox.Show("Failed to create base view: " & ex.Message)
End Try

' Loop through all components in the assembly to find sheet metal components
For Each oCompOcc As ComponentOccurrence In oAsmDoc.ComponentDefinition.Occurrences
   If TypeOf oCompOcc.Definition Is SheetMetalComponentDefinition Then
       Dim oSMDef As SheetMetalComponentDefinition = oCompOcc.Definition
       ' Check if the sheet metal component has a flat pattern
       If oSMDef.HasFlatPattern Then
           
		   Dim oOptions As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
           oOptions.Add("SheetMetalFoldedModel", False) ' Specify flat pattern
	
		   ' Define the placement point for the flat pattern view
           Dim oFlatPatternPoint As Point2d = oTG.CreatePoint2d(15, 15) ' Adjust position as necessary
           'MessageBox.Show("Code ok to this point")   	         
           ' Add the flat pattern view to the sheet
           Dim oFlatPatternView As DrawingView = oSheet.DrawingViews.AddBaseView(
               oCompOcc,
               oFlatPatternPoint,
               0.5, 
               ViewOrientationTypeEnum.kDefaultViewOrientation, 
               DrawingViewStyleEnum.kHiddenLineRemovedDrawingViewStyle,
               Nothing, Nothing, oOptions)
       End If
   End If
Next

' Update the drawing to reflect all changes
oDrawingDoc.Update()

 

 

 

 

 

0 Likes
Message 7 of 8

BM_Ashraf
Advocate
Advocate
Accepted solution

Hi,

This one should do it

' Declare document variables
Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument
If oAsmDoc.DocumentType <> kAssemblyDocumentObject Then
    MessageBox.Show("This script is only valid for assembly documents.", "Invalid Document Type")
    Return
End If

' Set the template path and create a new drawing document
Dim oTemplatePath As String = ThisApplication.FileOptions.TemplatesPath

Dim oDrawingDoc As DrawingDocument = ThisApplication.Documents.Add( _
	DocumentTypeEnum.kDrawingDocumentObject, oTemplatePath & "\abb_a3.idw", True)

' Add an assembly view to the drawing
Dim oSheet As Sheet = oDrawingDoc.Sheets(1)
Dim oBaseView As DrawingView

'Dim oViews As DrawingViews = oAsmDoc.ActiveSheet.DrawingViews
Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
Try
	Dim oBaseViewPoint As Point2d = oTG.CreatePoint2d(5, 15) ' Position of the base view on the sheet
	oBaseView = oSheet.DrawingViews.AddBaseView(
				oAsmDoc, 
				oBaseViewPoint,
				0.5,
				ViewOrientationTypeEnum.kDefaultViewOrientation, 
				DrawingViewStyleEnum.kHiddenLineRemovedDrawingViewStyle)
				
Catch ex As Exception
	MessageBox.Show("Failed to create base view: " & ex.Message)
End Try

' Loop through all components in the assembly to find sheet metal components
For Each oCompOcc As ComponentOccurrence In oAsmDoc.ComponentDefinition.Occurrences
   If TypeOf oCompOcc.Definition Is SheetMetalComponentDefinition Then
       Dim oSMDef As SheetMetalComponentDefinition = oCompOcc.Definition
       ' Check if the sheet metal component has a flat pattern
       If oSMDef.HasFlatPattern Then
           
		   Dim oOptions As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
           oOptions.Add("SheetMetalFoldedModel", False) ' Specify flat pattern
	
		   ' Define the placement point for the flat pattern view
           Dim oFlatPatternPoint As Point2d = oTG.CreatePoint2d(15, 15) ' Adjust position as necessary
           'MessageBox.Show("Code ok to this point")   	         
           ' Add the flat pattern view to the sheet
           Dim oFlatPatternView As DrawingView = oSheet.DrawingViews.AddBaseView(
               oCompOcc.ReferencedDocumentDescriptor.ReferencedDocument,
               oFlatPatternPoint,
               0.5, 
               ViewOrientationTypeEnum.kDefaultViewOrientation, 
               DrawingViewStyleEnum.kHiddenLineRemovedDrawingViewStyle,
               Nothing, Nothing, oOptions)
       End If
   End If
Next

' Update the drawing to reflect all changes
oDrawingDoc.Update()
If this solved your problem, or answered your question, please click ACCEPT SOLUTION.

Blue Mech

Add-ins for Inventor!

Message 8 of 8

Covello_O
Enthusiast
Enthusiast

Yes! Now it finally works. 🙂

Many thanks!

0 Likes