Obtaining the ActiveDocument for an AddIn converted from a VBA macro

Obtaining the ActiveDocument for an AddIn converted from a VBA macro

SteveLainson6531
Contributor Contributor
2,776 Views
5 Replies
Message 1 of 6

Obtaining the ActiveDocument for an AddIn converted from a VBA macro

SteveLainson6531
Contributor
Contributor

Hello,

 

Im just dipping into VB. net AddIns from usually using VBA macros and I'm strugging to get an existing macro working. The task is simple - obtaining the current active document and testing it for type but I just cant seem to find a way of using the .Activedocument property inherited from my macro:

 

So the code behind my form starts with that below, however the line oDoc = Inventor.activedocument is not being accepted.

 

Any ideas where I am going wrong please?

 

TIA

 

 

 

Imports Inventor


Public Class FrmPointsHarvest
    Dim oDoc As Document

    Private Sub FrmPointsHarvest_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        
        ' Set a reference to the active document. 
        oDoc = Inventor.activedocument

        'if a part or assembly
        Select Case oDoc.DocumentType
            Case 12290
                Part_Type = "IPT"
                oPartDoc = oDoc
                BtnHarvest.Enabled = True
            Case 12291
                Part_Type = "IAM"
                oAssyDoc = oDoc
                BtnHarvest.Enabled = True
            Case Else
                Part_Type = "Other"
                BtnHarvest.Enabled = False
                MsgBox(oDoc.DocumentType & " is Open. You must open an Inventor Part (.IPT) or Assembly (.IAM) file.", vbOKOnly)
                Me.Close()
        End Select

    End Sub

 

I need the oDoc setting so I can subsequently extract 3D points from each part in the assy or part like this, but without a reference to oDoc Im stuck !

 

Public Sub Export3DSketchPoints(oDoc As Inventor.PartDocument)
   'This sub extracts SKETCHPOINTS from a PART file
   
    Set oDef = oDoc.ComponentDefinition
   
    Dim o3DSketchs As Sketches3D
    Dim o3DSketch As Sketch3D
    Dim o3DSketchPoint As SketchPoint3D
    Dim Pnt As Point

    Set o3DSketchs = oDef.Sketches3D 'get all 3DSketches in this part

    Dim nRow As Integer
    nRow = 1
    
    For Each o3DSketch In o3DSketchs 'each 3d sketch
        For Each o3DSketchPoint In o3DSketch.SketchPoints3D
                Set Pnt = o3DSketchPoint.Geometry
                oSheet.Cells(nRow, 1) = conv(Pnt.X)
                oSheet.Cells(nRow, 2) = conv(Pnt.Y)
                oSheet.Cells(nRow, 3) = conv(Pnt.Z)
                nRow = nRow + 1
        Next o3DSketchPoint
    Next o3DSketch

End Sub

 

 

0 Likes
Accepted solutions (1)
2,777 Views
5 Replies
Replies (5)
Message 2 of 6

MechMachineMan
Advisor
Advisor

You need to connect to the inventor application before you can just call it.

 

 


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 3 of 6

CadUser46
Collaborator
Collaborator

Im not sure how that would have even worked in VBA.  As mechmachine wrote you need to get a hook to the application before you can do anything.  It would be something like this. (excuse my .net)

 

Dim m_invApp as inventor.application = thisapplication

Dim odoc as document = m_invApp.activedocument


Did you find this reply helpful ? If so please use the Accept as Solution or Kudos button below.

---------------------------------------------------------------------------------------------------------------------------
Inventor 2010 Certified Professional
Currently using 2023 Pro
0 Likes
Message 4 of 6

mikazakov
Advocate
Advocate
Accepted solution

Much better use constructor of class. The constructor call when you create instance of class automatically. Constructor will require pass the Inventor.Application object from you AddIn server.

 

Public Class Form1
    Private InvApp As Inventor.Application
    'Constructor
    Public Sub New(InvApp As Inventor.Application)
        ' Этот вызов является обязательным для конструктора.
        InitializeComponent()
        Me.InvApp = InvApp

        Dim Doc As Inventor.Document = InvApp.ActiveDocument

        Select Case Doc.DocumentType
            Case Inventor.DocumentTypeEnum.kPartDocumentObject
                Dim PartDoc As Inventor.PartDocument = DirectCast(Doc, Inventor.PartDocument)

            Case Inventor.DocumentTypeEnum.kAssemblyDocumentObject
                Dim AssDoc As Inventor.AssemblyDocument = DirectCast(Doc, Inventor.AssemblyDocument)

        End Select

    End Sub
End Class
Message 5 of 6

SteveLainson6531
Contributor
Contributor

Thanks Mikazakov,

 

Your solution worked. I've been following the Inventor AddIn creation lessons here and using the .Net Inventor Addin template this creates  the following code in the StandardAddInServer.vb file which I assumed would have made the connection to the Inventor application, but obviously not.

 

Thanks for taking to answer my question, its very much appreciated.

 

 

 

Imports Inventor
Imports System.Runtime.InteropServices
Imports Microsoft.Win32

Namespace PointsHarvester
    <ProgIdAttribute("PointsHarvester.StandardAddInServer"), _
    GuidAttribute("88601d5c-de0b-46e6-889a-5e03ac9c23ab")> _
    Public Class StandardAddInServer
        Implements Inventor.ApplicationAddInServer

        Private m_inventorApplication As Inventor.Application


#Region "ApplicationAddInServer Members"

        Public Sub Activate(ByVal addInSiteObject As Inventor.ApplicationAddInSite, ByVal firstTime As Boolean) Implements Inventor.ApplicationAddInServer.Activate

            m_inventorApplication = addInSiteObject.Application

        End Sub
Message 6 of 6

mikazakov
Advocate
Advocate

 

I know English is not perfect. I sometimes difficult to understand written.

You try to make the text easier, please.

 

Are you can't debug your code?

If its you need, that I say next:

If you read only Ekins manual, that you can't understand how launch AddIn to "Debug" of Visual Studio.

If you generate you vb.net code of addin from Autodesk template, you must do next steps.

Before starting debugging, you need:

  1. Open option of project and set the debug option to inventor.exe:1.png
  2. open your *.addin file from your vb. net project for edit.
  3. find Assembly tag and write full path to you compiled dll of addin (folder of you vb. net project: bin/debug/ "you dl"l), for instance my sample next text format: <Assembly>D:\1\ESKDspecia.dll</Assembly>
  4. Save and copy *.addin-file to folder when inventor do search file of addin, for instance my computer: C:\Users\KazakovMB\AppData\Roaming\Autodesk\Inventor 2016\Addins
  5. Add break point to you code, end click start program to Visual Studio

See API help from Inventor. There additonal info for work with Assembly tag.

0 Likes