Inventor Drawing: How to project a WorkAxis from a Part or an Assembly to a DrawingSketch?

Issue

I would like to create a SketchLine inside a DrawingSketch that represents my WorkAxis inside the referenced assembly document.

Solution

The following VB.NET example shows how to retrieve a WorkAxis inside an Assembly and project it onto a newly created DrawingSketch as a SketchLine. To run the example create an axis named "Work Axis1" in the WorkAxes of the assembly.

This VB.NET example is from a button on a form in a standalone exe that connects to Inventor from out of process.

 

Imports Inventor
    Public Class Form1
        Dim m_inventorApp As Inventor.Application = Nothing
 
        Private Sub Button1_Click(ByVal sender As System.Object,  
               ByVal e As System.EventArgs) Handles Button1.Click
 
            Try ' Try to get an active instance of Inventor
 
                Try
                    m_inventorApp = System.Runtime.InteropServices.
                      Marshal.GetActiveObject("Inventor.Application")
 
                Catch ' If not active, create a new Inventor session
 
                    Dim inventorAppType As Type = System.Type.
                            GetTypeFromProgID("Inventor.Application")
 
                    m_inventorApp = System.Activator.CreateInstance(
                                                     inventorAppType)
 
                    'Must be set visible explicitly
                    m_inventorApp.Visible = True
 
                End Try
 
            Catch
                System.Windows.Forms.MessageBox.Show(
                          "Error: couldn't create Inventor instance")
            End Try
 
            ProjectAxisToDrawing()
 
        End Sub
 
        Public Sub ProjectAxisToDrawing()
 
            If m_inventorApp.Documents.Count = 0 Then
                Exit Sub
            End If
 
            If m_inventorApp.ActiveDocument.DocumentType  
                         DocumentTypeEnum.kDrawingDocumentObject Then
                Exit Sub
            End If
 
            Dim oDrawingDoc As DrawingDocument
            oDrawingDoc = m_inventorApp.ActiveDocument
 
            Try
                Dim oView As DrawingView
                oView = oDrawingDoc.ActiveSheet.DrawingViews(1)
 
                'Retrieve referenced assembly
                Dim oAsmDoc As AssemblyDocument
                oAsmDoc = oView.ReferencedDocumentDescriptor.
                                              ReferencedDocument
 
                'Retrieve WorkAxis
                Dim oWorkAxis As WorkAxis
                oWorkAxis = oAsmDoc.ComponentDefinition.WorkAxes.
                                                   Item("Work Axis1")
 
                Dim oStartPoint As Point = m_inventorApp.
                               TransientGeometry.CreatePoint(0, 0, 0)
                Dim oEndPoint As Point = m_inventorApp.
                               TransientGeometry.CreatePoint(0, 0, 0)
 
                'Retrieve Axis bound points (graphical points)
                Call oWorkAxis.GetSize(oStartPoint, oEndPoint)
 
                'Retrieve points coordinates in sheet space
                Dim oSheetPoint1 As Point2d
                Dim oSheetPoint2 As Point2d
 
                oSheetPoint1 = oView.ModelToSheetSpace(oStartPoint)
                oSheetPoint2 = oView.ModelToSheetSpace(oEndPoint)
 
                'Add new DrawingSketch
                Dim oSketch As DrawingSketch
                oSketch = oView.Sketches.Add
 
                'Retrieve points coordinates in sketch space
                Dim oInSketchPoint1 As Point2d
                Dim oInSketchPoint2 As Point2d
 
                oInSketchPoint1 = oSketch.SheetToSketchSpace(
                                                        oSheetPoint1)
                oInSketchPoint2 = oSketch.SheetToSketchSpace(
                                                        oSheetPoint2)
 
                'Create 2 sketch points
                Dim oSketchPoint1 As SketchPoint
                Dim oSketchPoint2 As SketchPoint
 
                'Open Sketch in Edit Mode
                oSketch.Edit()
 
                oSketchPoint1 = oSketch.SketchPoints.Add(
                                                     oInSketchPoint1)
                oSketchPoint2 = oSketch.SketchPoints.Add(
                                                     oInSketchPoint2)
 
                'Create sketch line that represents the axis
                Call oSketch.SketchLines.AddByTwoPoints(
                                        oSketchPoint1, oSketchPoint2)
 
                'Close Sketch
                Call oSketch.ExitEdit()
            Catch ex As SystemException
                MsgBox(ex.ToString())
            End Try
 
        End Sub
    End Class

Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading