Access drawing properties outside AutoCAD

By Augusto Goncalves

We can use either MFC or ATL to create a client VC++ application of the COM server to access that information. In the attached sample, MFC is used. We should add a class Iproperties which wraps the methods and properties of the COM server from DwgPropsX.DLL with MFC class wizard into the project skeleton. Then we may find the ProgID of the COM server from Windows registry and find the CLSID with function CLSIDFromProgID. Of course, please make sure the COM server has been registered. The most common way to register Windows services is to use RegSvr32.Exe. We can also register them programmatically by calling it as a system command or with other APIs. Please find more information regarding this from MSDN.

The project supports multiple documents. When we open a DWG file with a view available, if it contains drawing properties, they will display in the current view. We can display properties of more drawings in multiple view windows. Anyway, the most important part is the following appended global function which contains COM initialization, ProgID to CLSID conversion, dispatch interface creation, properties retrieve, and COM clean up.

Full sample project.

// Global function to retrieve the drawing properties.
// In
//  LPCTSTR szFileName: Full path and name of a DWG file
// Output
//  LPCTSTR: A string containing all the property information
//  delimited by new line symbol 'n'
LPTSTR getSummaryInformation(LPCTSTR szFileName)
{
  LPTSTR szSummInfo = NULL;
  CString str;
  IProperties dwgProp;
  HRESULT hr = NOERROR;
  CLSID clsid;
  LPDISPATCH pDisp=NULL;
 
  hr = CoInitialize(NULL);
  if (FAILED(hr))
    return NULL;
 
  hr = ::CLSIDFromProgID(L"DwgPropsX.Properties", &clsid);
  if(FAILED(hr))
    return NULL;
 
  VERIFY(dwgProp.CreateDispatch(clsid) == TRUE);
 
  dwgProp.Load(szFileName);
 
  str = "Drawing Summary Information:n";
  str = str + (CString)"nTitle: " +
    (CString)dwgProp.GetTitle();
  str = str + (CString)"nSubject: " +
    (CString)dwgProp.GetSubject();
  str = str + (CString)"nAuthor: " +
    (CString)dwgProp.GetAuthor();
  str = str + (CString)"nComments: " +
    (CString)dwgProp.GetComments();
  str = str + (CString)"nKeywords: " +
    (CString)dwgProp.GetKeywords();
  str = str + (CString)"nLast Saved By: " +
    (CString)dwgProp.GetLastSavedBy();
  str = str + (CString)"nRevision Number: " +
    (CString)dwgProp.GetRevisionNumber();
  str = str + (CString)"nHyperlink Base: " +
    (CString)dwgProp.GetHyperlinkBase();
  for(int i=0; i

Comments

5 responses to “Access drawing properties outside AutoCAD”

  1. Yan Sid Avatar
    Yan Sid

    Simple question but I have not been able to find a simple solution.
    In windows explorer I can right click on a .dwg file and view, under the “Custom” tab, all of the custom Drawing Properties I’ve added (in AutoCAD Drawing Utilites -> Drawing Properties). Its great I can copy info from outside autocad with my mouse.
    What I really want to do is extract that information, read only, with a vba or powershell script, and send it to a text file with the same name as the drawing. Unfortuantely I do not currently have visual studio and can’t compile the above.
    How can you, outside autocad, pipe a custom property to a text file? Something like
    cmd> customApp A-Test.dwg “custom Title” “target output directory”
    Creates A-Test.txt with text “my custom title here”

  2. Hi Yan Sid
    Without Visual Studio you cannot compile this code… sorry. But the same should be available via VBA/VB6 COM Automation, just add DwgProps component.
    If you like more features, then try the AutoCAD Console, you can automate it with BAT files and some scripts (.scr files).
    Regards,
    Augusto Goncalves

  3. Yan Sid Avatar
    Yan Sid

    Thanks!

  4. Rodrigo_V Avatar
    Rodrigo_V

    I compiled your code in Visual Studio Community. It crashes whenever trying to open a Autocad 2015 file. Is there a package I’m missing? Not too familiar to this blog, I’m guessing your code is built upon assuming you already have another package installed?

  5. Rodrigo, this sample is in C++ (so it doesn’t uses packages), but you need to compile it using the COM references. It also should not require you to start AutoCAD (as it uses only the COM API for Dwg Props). Finally the VS Community doesn’t include MFC (I believe) that is required for CString (among other Windows features).

Leave a Reply to Yan SidCancel reply

Discover more from Autodesk Developer Blog

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

Continue reading