Use Inventor API to Get Drawing View Information Such as Scale , Referenced File etc

By Barbara Han

From a drawing document you can iterate through the Sheet collection object and from each Sheet object, retrieve all the drawing views, and from the drawing view you can extract further information. The sample code (VBA and C++) shown below prints information such as the scale of drawing view and file being referred by that view.

VBA code sample:

Sub getViewsInfo()
    Dim oDoc As Document
    Set oDoc = ThisApplication.ActiveDocument
   
    If oDoc.DocumentType = kDrawingDocumentObject Then
   
        Dim oDrgDoc As DrawingDocument
        Set oDrgDoc = oDoc
             
        Dim oSheets As Sheets
        Set oSheets = oDrgDoc.Sheets
        Dim oSheet As Sheet
       
        For Each oSheet In oSheets
            Dim oDrgViews As DrawingViews
            Set oDrgViews = oSheet.DrawingViews
            Dim oDrgView As DrawingView
            Dim msg As String
            Dim i As Integer
            i = 1
            For Each oDrgView In oDrgViews
                Dim scl As Double
                scl = oDrgView.Scale
                Dim oRefFileDesc As ReferencedFileDescriptor
                Set oRefFileDesc = oDrgView.ReferencedFile
               

                If (TypeOf oRefFileDesc.ReferencedDocument Is PartDocument) Then
                    Dim oPartDoc As PartDocument
                    Set oPartDoc = oRefFileDesc.ReferencedDocument
                ElseIf (TypeOf oRefFileDesc.ReferencedDocument Is AssemblyDocument) Then
                    Dim oAsmDoc As AssemblyDocument
                    Set oAsmDoc = oRefFileDesc.ReferencedDocument
                Else
                    Dim oPresDoc As PresentationDocument
                    Set oPresDoc = oRefFileDesc.ReferencedDocument
                End If
               
               
                msg = "View No: " & i & vbCr & _
                           "File Refered: " & oRefFileDesc.FullFileName & vbCr & _
                           "View Scale  : " & scl
                i = i + 1

               MsgBox msg
            Next
        Next
    End If
End Sub

C++ code sample:

HRESULT getViewsInfo()

{

 

  HRESULT hr = NOERROR;

 

  // Get active Inventor application

  TCHAR Str[_MAX_PATH];

 

  CLSID InvAppClsid;

  hr = CLSIDFromProgID (L"Inventor.Application", &InvAppClsid);

  if (FAILED(hr)) return hr;

 

  // Either create a new instance of the application or latch on to the currently active one.

  _tprintf_s (_T("Would you like to create a new Inventor application? (y/n) _: "));

  _tscanf_s (_T("%ls"), Str, _MAX_PATH);

 

  CComPtr<IUnknown> pInvAppUnk;

  if (toupper (Str[0]) == _T(‘Y’))

  {

    hr = CoCreateInstance (InvAppClsid, NULL, CLSCTX_LOCAL_SERVER, __uuidof(IUnknown), (void **) &pInvAppUnk);

    if (FAILED (hr))

      _tprintf_s (_T("*** Failed to create a new Inventor application ***\n"));

  }

  else

  {

    hr = ::GetActiveObject (InvAppClsid, NULL, &pInvAppUnk);

    if (FAILED (hr))

      _tprintf_s (_T("*** Could not get hold of an active Inventor application ***\n"));

  }

  if (FAILED(hr)) return hr;

 

  CComPtr<Application> m_pApplication;

  hr = pInvAppUnk->QueryInterface (__uuidof(Application), (void **) &m_pApplication);

  if (FAILED(hr)) return hr;

 

  CComPtr<Document> pDoc;

  hr=m_pApplication->get_ActiveDocument(&pDoc);

 

  if (pDoc != NULL && pDoc->DocumentType==kDrawingDocumentObject)

  { 

    CComQIPtr<DrawingDocument> pDrgDoc(pDoc);

 

    // Get sheets collection

    CComPtr<Sheets> pSheets;

    hr=pDrgDoc->get_Sheets(&pSheets);

    long nSheets=0;

    hr=pSheets->get_Count(&nSheets);

 

    // iterate through sheets

    for (long i =1 ; i <= nSheets ; i++)

    {

      CComPtr<Sheet> pSheet;

      hr=pSheets->get_Item(_variant_t(i),&pSheet);

 

      // Get drawing views collection

      CComPtr<DrawingViews> pDrawingViews;

      hr=pSheet->get_DrawingViews(&pDrawingViews);

 

      // iterate through drawing views

      long nViews=0;

      hr=pDrawingViews->get_Count(&nViews);

      for(long j =1 ; j <= nViews ; j++)

      {

        CComPtr<DrawingView> pDrawingView;

        hr=pDrawingViews->get_Item(j,&pDrawingView);

        double dScl = 0.0;

&
#160;       hr=pDrawingView->get_Scale(&dScl);

 

        CComPtr<ReferencedFileDescriptor> pRefFileDesc;

        hr=pDrawingView->get_ReferencedFile(&pRefFileDesc);

 

        CComPtr<IUnknown> pUnk;

        hr=pRefFileDesc->get_ReferencedDocument(&pUnk); 

 

        // Now the referenced documnet can be ipt,iam or ipn

        // cast pUnk to document

        CComQIPtr<Document> pRefDoc(pUnk);

        if (pRefDoc->DocumentType==kPartDocumentObject)

          CComQIPtr<PartDocument> pPartDoc(pRefDoc);

        else if (pRefDoc->DocumentType==kAssemblyDocumentObject)

          CComQIPtr<AssemblyDocument> pAssmDoc(pRefDoc);

        else

          CComQIPtr<PresentationDocument> pPresDoc(pRefDoc);

 

        // Get the display name of reference document

        CComBSTR bstrFileName;

        hr=pRefFileDesc->get_FullFileName(&bstrFileName);

 

        _tprintf_s(_T("View No: %ld\nFile Refered: %s\nView Scale: %lf\n"), j,bstrFileName,dScl); 

      }

    }

  }

  return hr;

}


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading