How to project geometry from a base view to a sketch in a drawing

piotrekdoro94
Enthusiast
Enthusiast

How to project geometry from a base view to a sketch in a drawing

piotrekdoro94
Enthusiast
Enthusiast

Hi, I have a sheet metal part with a flat pattern. Now I want to create a drawing with a single base view of that flat pattern, then create a sketch on that base view and project all the edges.

 

Unfortunately, I can't figure out how to project flat pattern edges. I would really appreciate if someone could help me correct my code or provide a simplified example showing how to project a single edge from a part shown in a base view to a sketch, created on the same base view.

 

Ma part and its flat pattern look like this:

InletPipe.png

InletPipeFlatPattern.png

 Here is my code:

Imports Inventor
Imports System.Runtime.InteropServices
Imports System.Text.RegularExpressions
Imports System.Windows.Forms
Imports System.IO
Imports System.Xml.Serialization
Imports System.Security.Cryptography
Imports System.Diagnostics.Contracts


Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Try



            Dim InventorApp As Inventor.Application
            Dim oDrawingDoc As DrawingDocument


            InventorApp = Marshal.GetActiveObject("Inventor.Application")
            oDrawingDoc = InventorApp.Documents.Add(DocumentTypeEnum.kDrawingDocumentObject,, True) 'create drawing
            oDrawingDoc.Activate()

            Try
                InventorApp = Marshal.GetActiveObject("Inventor.Application")
                'MessageBox.Show("Connected To Inventor session")
            Catch ex As Exception
                MessageBox.Show("Failed to connect to inventor session")
            End Try

            oDrawingDoc.Sheets(1).Orientation = PageOrientationTypeEnum.kLandscapePageOrientation 'Change sheet orientation to landscape
            oDrawingDoc.Sheets(1).Size = DrawingSheetSizeEnum.kA3DrawingSheetSize 'Change sheet size to A4

            Dim baseViewOptions As NameValueMap

            baseViewOptions = InventorApp.TransientObjects.CreateNameValueMap() 'Create NameValueMap to use as additional option for view generation
            baseViewOptions.Add("SheetMetalFoldedModel", False) 'Add flat pattern on to additional opiots
            oDrawingDoc.Sheets(1).DrawingViews.AddBaseView(InventorApp.Documents(1), InventorApp.TransientGeometry.CreatePoint2d(), 1, ViewOrientationTypeEnum.kFlatPivot180ViewOrientation, DrawingViewStyleEnum.kHiddenLineRemovedDrawingViewStyle, "Flat pattern",, baseViewOptions) 'Create bbase view with a flat pattern

            Dim baseView As DrawingView
            Dim baseViewHeight As Double
            Dim baseViewWidth As Double
            Dim sheetHeight As Double
            Dim sheetWidth As Double

            baseView = oDrawingDoc.Sheets(1).DrawingViews(1) 'get base view
            baseViewHeight = baseView.Height 'get base view height
            baseViewWidth = baseView.Width 'get base view width
            sheetHeight = oDrawingDoc.Sheets(1).Height 'get sheet height
            sheetWidth = oDrawingDoc.Sheets(1).Width 'get sheet width

            baseView.Position = InventorApp.TransientGeometry.CreatePoint2d(sheetWidth / 2, sheetHeight / 2) 'Move base view to center of sheet

            Dim baseViewReference As PartDocument
            Dim baseViewReferenceCompDef As SheetMetalComponentDefinition
            Dim projectedEdge As Edge

            baseViewReference = baseView.ReferencedDocumentDescriptor.ReferencedDocument
            baseViewReferenceCompDef = baseViewReference.ComponentDefinition

            For Each projectedEdge In baseViewReferenceCompDef.FlatPattern.SurfaceBodies(1).Edges
                Dim drawCurves As DrawingCurvesEnumerator
                drawCurves = baseView.DrawingCurves(projectedEdge)

                Dim drawCurve As DrawingCurve
                For Each drawCurve In drawCurves
                    Dim ent As SketchEntity
                    ent = baseView.Sketches(1).AddByProjectingEntity(drawCurve) '--->!!!Program crashes here on first loop!!!<---
                Next
            Next

            oDrawingDoc.Update()

        Catch ex As Exception
            MsgBox("Generation failed")
        End Try
    End Sub


End Class

 I've attached the .ipt file and a .zip file with the entire project. In order to run it first open the .ipt file first and then run the code. It finds reference to the ipt file by looking for the first file in active documents collection.

0 Likes
Reply
Accepted solutions (1)
226 Views
1 Reply
Reply (1)

piotrekdoro94
Enthusiast
Enthusiast
Accepted solution

Nevermind, it seems I was overthinking it (and also, I've accidently commented out a line creating the sketch in the first place), I've managed to get the desired effect by using this code:

 

Imports Inventor
Imports System.Runtime.InteropServices
Imports System.Text.RegularExpressions
Imports System.Windows.Forms
Imports System.IO
Imports System.Xml.Serialization
Imports System.Security.Cryptography
Imports System.Diagnostics.Contracts


Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Try



            Dim InventorApp As Inventor.Application
            Dim oDrawingDoc As DrawingDocument


            InventorApp = Marshal.GetActiveObject("Inventor.Application")
            oDrawingDoc = InventorApp.Documents.Add(DocumentTypeEnum.kDrawingDocumentObject,, True) 'create drawing
            oDrawingDoc.Activate()

            Try
                InventorApp = Marshal.GetActiveObject("Inventor.Application")
                'MessageBox.Show("Connected To Inventor session")
            Catch ex As Exception
                MessageBox.Show("Failed to connect to inventor session")
            End Try

            oDrawingDoc.Sheets(1).Orientation = PageOrientationTypeEnum.kLandscapePageOrientation 'Change sheet orientation to landscape
            oDrawingDoc.Sheets(1).Size = DrawingSheetSizeEnum.kA3DrawingSheetSize 'Change sheet size to A4

            Dim baseViewOptions As NameValueMap

            baseViewOptions = InventorApp.TransientObjects.CreateNameValueMap() 'Create NameValueMap to use as additional option for view generation
            baseViewOptions.Add("SheetMetalFoldedModel", False) 'Add flat pattern on to additional opiots
            oDrawingDoc.Sheets(1).DrawingViews.AddBaseView(InventorApp.Documents(1), InventorApp.TransientGeometry.CreatePoint2d(), 1, ViewOrientationTypeEnum.kFlatPivot180ViewOrientation, DrawingViewStyleEnum.kHiddenLineRemovedDrawingViewStyle, "Flat pattern",, baseViewOptions) 'Create bbase view with a flat pattern

            Dim baseView As DrawingView
            Dim baseViewHeight As Double
            Dim baseViewWidth As Double
            Dim sheetHeight As Double
            Dim sheetWidth As Double

            baseView = oDrawingDoc.Sheets(1).DrawingViews(1) 'get base view
            baseViewHeight = baseView.Height 'get base view height
            baseViewWidth = baseView.Width 'get base view width
            sheetHeight = oDrawingDoc.Sheets(1).Height 'get sheet height
            sheetWidth = oDrawingDoc.Sheets(1).Width 'get sheet width

            baseView.Position = InventorApp.TransientGeometry.CreatePoint2d(sheetWidth / 2, sheetHeight / 2) 'Move base view to center of sheet

            Dim baseViewSketch As DrawingSketch
            Dim drawCurve As DrawingCurve

            baseViewSketch = baseView.Sketches.Add()

            For Each drawCurve In baseView.DrawingCurves
                baseViewSketch.AddByProjectingEntity(drawCurve)
            Next

oDrawingDoc.Update()

        Catch ex As Exception
            MsgBox("Generation failed")
        End Try
    End Sub


End Class

 

0 Likes