Viewing, Editing, and Saving AutoCAD Files in Microsoft Office Visio

 

Andrew May
Microsoft Corporation

December 2003

Applies to:
    Microsoft Office Visio 2003

Summary: Use add-ons included with Microsoft Office Visio 2003 to automate importing Autodesk AutoCAD files into Visio drawings, either as view-only (read-only) objects or as editable Visio shapes. You can also use add-ons to automate importing AutoCAD block libraries and even save Visio drawings in AutoCAD file formats (.dwg and .dxf). (7 printed pages)

Contents

Introduction
Running the Add-ons
Open a View-only Copy of an AutoCAD File in Visio
Save a Visio Drawing as an AutoCAD File
Open AutoCAD Drawings as Visio Files with Editable Shapes

Introduction

Users who create drawings in Microsoft Office Visio often create drawings in AutoCAD as well or work with those who do. Visio offers a series of add-ons that make it simple for users to import AutoCAD files into Visio and save Visio drawings as AutoCAD files. These add-ons are installed with Visio and let users use Visio to open and convert drawings created in AutoCAD file formats (.dwg and .dxf), as well as save Visio drawings in those AutoCAD formats. These add-ons also make it easy to convert AutoCAD symbol libraries (known as "block" libraries) into Visio masters.

There are six add-ons that make working with AutoCAD files in Visio easy. They are:

  • Open AutoCAD Drawing
  • Insert AutoCAD Drawing
  • Export to AutoCAD DWG
  • Export to AutoCAD DXF
  • Convert AutoCAD Drawings
  • Convert AutoCAD Library

Each of these add-ons appears in the Visio user interface and is also available through the Visio Automation interface. Using and automating each add-on is discussed in detail later in this article.

Running the Add-ons

As with all add-ons, each AutoCAD add-on is added to the Addons collection of the Application object. Each Addon object in the collection can be retrieved from the collection by name or index. Most importantly, each add-on has a Run method, which enables Visio to run it in response to formula evaluation or actions in the user interface.

You can run an add-on in the following ways:

  • By using the Run method of an Addon object.
  • From a formula contained in a ShapeSheet cell.
  • From the Macro or Add-Ons submenus.
  • By using the UIObject object model.
  • By using the Office command bar object model.

Each add-on's Run method takes a single optional argument: a string, which can actually be used to pass one or more optional command-line parameters (for example, the units of the Visio drawing, the scale at which to open the drawing, or the path to the specified file). Valid strings for each add-on's Run method are discussed in detail later in this article.

Using universal names

Many elements that appear in the Visio user interface have local and universal names. Universal names are names that remain constant, regardless of which language version of Visio you use. By using universal names rather than local names in your code, you can avoid having to localize (translate) your code for each language version of Visio.

The add-ons can be called either by using their indexes or their English or universal names, which are listed in the following table.

Table 1. Add-ons indexes and English or universal names (universal names that differ from the English names are in bold)

English name Universal name
Open AutoCAD Drawing Open AutoCAD Drawing
Insert AutoCAD Drawing Insert AutoCAD Drawing
Export to AutoCAD Export to AutoCAD DWG
Export to AutoCAD DXF Export to AutoCAD DXF
Convert CAD Drawings... Convert AutoCAD Drawings
Convert CAD Library... Convert AutoCAD Library

Using universal names and universal properties such as NameU and ItemU makes your code more efficient and usable across locales. For more information about using universal names in your Visio solutions, see Using Universal Names and Syntax to Localize Your Microsoft Office Visio Solutions.

Determining if an add-on is installed

All six of the AutoCAD add-ons are included in a typical or complete installation of Visio. However, users who perform a custom installation of Visio may choose not to install the add-ons. To determine if an add-on is actually installed, use the following function:

Public Function AddonExists(strAddonName As String, blnUniversalName as Boolean) As Boolean 
   Dim vsoAddon   As Visio.Addon
 
   AddonExists = False

   On Error Resume Next

   If (blnUniversalName) Then
     Set vsoAddon = Application.Addons.ItemU(strAddonName)
   Else
     Set vsoAddon = Application.Addons.Item(strAddonName)
   End If

   If Not (vsoAddon Is Nothing) Then
     AddonExists = True
   End If

End Function

Note that the function uses the ItemU and Item properties. If the function is passed a string that represents the add-on's universal name, it uses the ItemU property (which returns an add-on based on its universal name) to determine if the add-on is installed; otherwise, the function uses the Item property to determine if the add-on is installed.

For more information about creating, installing, and running add-ons, and about how they differ from COM add-ins, see About Microsoft Office Visio Add-ons and COM Add-ins.

Open a View-only Copy of an AutoCAD File in Visio

The first two add-ons enable users to import view-only copies of AutoCAD drawings, either into a new Visio drawing or an existing Visio document. For example, you can import a drawing created in AutoCAD and mark-up (or "redline") the drawing with annotations and revisions in Visio. Or, you can insert an AutoCAD file into an existing Visio drawing to serve as a detail or reference, such as an AutoCAD floor plan beneath a Visio facilities-management diagram.

The Open AutoCAD Drawing add-on opens a copy of an AutoCAD (.dwg or .dxf) file in Visio as a read-only ActiveX object. The ActiveX control is opened in a new Visio document with a default name (for example, "Document1"). The original AutoCAD document is not altered.

The Insert AutoCAD Drawing add-on inserts a .dwg or .dxf file into the active page in an existing Visio document as a read-only ActiveX object. The active Visio application window must contain a document drawing page when this add-on is run (otherwise an error is generated).

When the AutoCAD file is opened by either add-on, the last saved active space (model or paper space) is displayed. For AutoCAD drawings representing two-dimensional models, the drawing is opened zoomed out so that all the contents can be viewed. For AutoCAD drawings representing three-dimensional models, the view opened in Visio is the last saved view of that model.

The Open AutoCAD Drawing and Insert AutoCAD Drawing add-ons create a view-only ActiveX control optimized to render an accurate visual representation of the original AutoCAD drawing. The Convert AutoCAD Drawings add-on (discussed later in this article) creates Visio shapes that can be edited but may not be as faithful a visual representation of the original AutoCAD shapes.

Both add-ons take command-line options, in the following format:

/token = value

where token is the option, and value is the option's value.

When you are using the Run method of both add-ons, you can pass these options as a single string constructed of the options, each separated by a space, in the following order:

/unit=VisUnitCodesFileName

Table 2. Command-line options

Option Option token Option value Description
unit "/unit=" VisUnitCode Optional. The unit code to be used with the Result property of the Cell object and similar approaches. The default is visPageUnits.

To specify the unit code to be used, you must include the literal token string ("/unit=") followed by a valid VisUnitCodes constant.

filename (none) FileName Optional string. The path or URL to the file to be opened. If no file name is specified, the Open File dialog box is displayed.

Note   If both a unit code and a file name are specified, the two values that specify the unit code (/unit=VisUnitCode) must be separated from the file name by a space. For example:

Application.Addons.ItemU("Open AutoCAD Drawing").Run("/unit=" & visInches & " Filename.dwg)" 

Note   The Visio user interface offers additional options for working with view-only AutoCAD drawings. Once you have imported the ActiveX object that represents the AutoCAD drawing into a Visio file, you can right-click the object, point to CAD Drawing Object, and then click Convert or Properties. The Convert CAD Drawing dialog box enables you to convert the view-only drawing into fully editable shapes, including selecting which layers of the drawing to convert. The CAD Drawing Properties dialog box enables you to choose which layers of the drawing to display, as well as to control the color and line weight of each layer. The functionality in the Convert CAD Drawing dialog box is not available programmatically.

Save a Visio Drawing as an AutoCAD File

With the Export to AutoCAD add-ons, you can easily save a Visio drawing in a native AutoCAD file format (.dwg or .dxf). This enables users to create files in Visio and work with other technical professionals who use AutoCAD.

The Export to AutoCAD DWG add-on saves the contents of the active Visio page to a .dwg file, while the Export to AutoCAD DXF add-on saves the contents of the active Visio page to a .dxf (ASCII) file. The active Visio application window must contain a document drawing page when either add-on is run (otherwise an error is generated).

Any masters present in the Visio drawing are converted to blocks.

The Run method of both add-ons takes a single argument, a string representing the path or URL of the file to save. If no file name is specified, the Save As dialog box is displayed.

The following example sets the active window to a document page, and then saves the specified Visio file page as an AutoCAD drawing (.dwg) file. (The example assumes the first Window object in the Windows collection of the Application object is a document window and that the document has a page named "Page-1". For the example to run properly, replace path with a string representing the path to a file, and file with the desired file name.

With Application.Windows.Item(1)
    .Activate
    .Page = "Page-1"
End With

Application.Addons.Item("Export to AutoCAD DWG").Run ("path\file.dwg")

These add-ons are equivalent to saving the file as a .dwg or .dxf file in the user interface. To save the files through the user interface, click Save As on the File menu. In the Save As dialog box, on the Save as type list, select AutoCAD Drawing (*.dwg) or AutoCAD Interchange (*.dxf).

Open AutoCAD Drawings as Visio Files with Editable Shapes

If you need to edit the shapes and blocks in the AutoCAD drawing once you've imported it into Visio, use the Convert AutoCAD add-ons. You can even import CAD symbol libraries (known as "block" libraries) into Visio and convert them to masters on a Visio stencil. Importing CAD libraries enables users to make use of block shapes created and stored in AutoCAD files, rather than having to create those shapes anew in Visio. For example, an engineer might want to reuse an extensive library of process engineering equipment blocks stored in AutoCAD files.

The Convert AutoCAD Drawings add-on converts a .dwg or .dxf file to a new Visio file with editable shapes. The original AutoCAD file is not altered.

Any blocks used on the drawing page in the drawing file are converted to Visio masters and placed on the document stencil. If the file contains no drawing information, a blank drawing page is created.

The Convert AutoCAD Library add-on converts blocks contained in a .dwg or .dxf file to editable masters in a new Visio stencil file. The original AutoCAD file is not altered.

Each block in the AutoCAD file is converted to a master on a new Visio stencil. Block attribute definitions are converted to custom properties of the resulting Visio master. If the block attribute is not hidden, a text field is created on the master to show the attribute. Any xdata (custom binary information) attached to a block is not converted or retained within Visio.

Drawing information contained in the AutoCAD file is not converted. If the file contains no blocks, an empty stencil is created.

Both add-ons take command-line options, in the following format:

/token = value

where token is the option, and value is the option's value.

When you are using the Run method of both add-ons, pass these options as a single string constructed of the options, each separated by a space, in the following order:

/unit=VisUnitCodes /scale=DoubleFileName

Table 3. Command-line options

Option Option token Option value Description
unit "/unit=" VisUnitCode Optional. The unit code to be used with the Result property of the Cell object and similar approaches. The default is visPageUnits.

To specify the unit code to be used, you must include the literal token string ("/unit=") followed by a valid VisUnitCodes constant.

scale "/scale=" Double Optional. The scale at which the file is converted. Must be a positive, nonzero number capable of being stored in a Visual Basic Double data type. The default is 1.0.

Note This number must be formatted with a period (.) as a decimal separator. Using any other character as a decimal separator (such as a comma) will result in an incorrect string.

To specify the scale at which the file is converted, you must include the literal token string ("/scale=") followed by a number in the appropriate format, as described earlier.

filename (none) FileName Optional string. The path or URL to the file to be opened. If no file name is specified, the Open File dialog box is displayed.

If more than one of these options is specified, the values that make up one option (such as /unit=VisUnitCode, /scale=Double, or FileName) must be separated from each other by a space. For example:

Application.Addons.ItemU("Open AutoCAD Drawing").Run _ &
          ("/unit=" &  visInches &  " /scale=0.25 Filename.dwg)"

Important   As noted above, the conversion scale must be expressed as a string where the scale number is formatted with a period (.) as the decimal separator. Therefore, using Visual Basic functions such as Cstr, Str, and Format, which convert numbers to strings using the user's region settings, will create a string that is formatted incorrectly. For example, using these functions with a European region setting would result in a scale number formatted with a comma as the decimal separator.

The following example opens an AutoCAD drawing (.dwg) file as a Visio drawing with editable shapes at the default units (page units) and a specified scale (0.25). It then saves the new Visio drawing in the same folder and with the same name as the original AutoCAD file.

Sub ConvertAutoCadeFileAndSave(strFileName as String, strPathToFile as string)
      Application.Addons.ItemU("Convert AutoCAD Drawings").Run ("/scale=0.25 " & _ 
      strPathToFile & strFilename & ".dwg")
   Documents.Item(Documents.Count).SaveAs (strPathToFile & strFileName & ".vsd")
 End Sub

To access these add-ons from the user interface, on the Tools menu, point to Add-ons, and then click Run Add-on. Select Convert CAD Drawings or Convert CAD Library, click OK, navigate to the file you want to convert, and then click Open.