CAD Management – Resource Style

by Mark Stefanchuk, CADmanage.com

Seth and I have been a somewhat focused on resources lately, but for good reason. Things like layers, blocks, and styles (lines, dimensions, tables) are assets that are created and recreated from project to project and it seems like this is a bit of extra work.

Of course we can use templates and standards, but there always seems to be a page setup or table style missing – one that we created for a project we were working on just last week. And boy wouldn’t it be great if I could get all of those things with a quick click.

The import of many of these assets is pretty straight forward code wise, so I’m going to “give away the store” (or just a small portion of the store) and show you how you can do this yourself. I’ll then introduce you to our next Exchange App that we expect will be ready by the end of November.

In this example I’m going to show you how to get text styles from an external DWT, DWS, or DWG file (the sourceDb) and bring these into the current DWG (the destDb). We will use VB.Net. Here’s the code listing. Comments embedded.

'use the following: 
Imports Autodesk.AutoCAD.Runtime 
Imports Autodesk.AutoCAD.ApplicationServices 
Imports CADAPP = Autodesk.AutoCAD.ApplicationServices.Application 
Imports Autodesk.AutoCAD.DatabaseServices 
Imports Autodesk.AutoCAD.EditorInput 

'string library_path => the file spec of the source dwt, dws, or dwg file 
'DuplicateRecordCloning clg => Replace or Ignore - how to handle text styles with the same name 
Private Sub cmr_getTextStyles(ByVal library_path As String, ByVal clg As DuplicateRecordCloning)
'connect to current database
Dim dm As DocumentCollection = CADAPP.DocumentManager
Dim doc As Document = dm.MdiActiveDocument
Dim ed As Editor = dm.MdiActiveDocument.Editor
Dim destDb As Database = dm.MdiActiveDocument.Database
Dim sourceDb As Database = New Database(False, True)

Try
    Using dl As DocumentLock = doc.LockDocument()
    'connect to the external dwt, dws, or dwg
    sourceDb.ReadDwgFile(library_path, System.IO.FileShare.Read, True, "")

    'create a container for our text styles
    Dim textsToClone As ObjectIdCollection = New ObjectIdCollection()
    Dim tm As Autodesk.AutoCAD.DatabaseServices.TransactionManager = sourceDb.TransactionManager

    'start a transaction with the external drawing
    Using tr As Transaction = tm.StartTransaction()

        'get the text style ids
        Dim ldt As TextStyleTable = TryCast(tm.GetObject(sourceDb.TextStyleTableId, _
            OpenMode.ForRead, False), TextStyleTable)

        'save the ids in an object collection
        For Each txtID As ObjectId In ldt
            textsToClone.Add(txtID)
        Next

        ' Add text styles to the current drawing
        Dim mapping As IdMapping = New IdMapping()
        sourceDb.WblockCloneObjects(textsToClone, destDb.TextStyleTableId, mapping, clg, False)
        'commit the sourceDb transaction (even for read)
        tr.Commit()
    End Using
End Using
Catch ex As Autodesk.AutoCAD.Runtime.Exception
'
End Try
End Sub

You can use this same pattern for dimension styles, multi-leader styles, table styles, line types, and page sets. Layers, and blocks are similar but require some extra effort.

So, what about that next app? Seth and I have been working on a new app for Autodesk Exchange and expect to have it published shortly. So, if you don’t want to tackle importing resources yourself then you can download our version. DWG Resource Importer consolidates these operations into a convenient palette allowing you to quickly import resources from templates, standard files, and drawing files. Here’s what you can expect to see.

Select the resources to import – layers, blocks, line styles, dimension, and more…

Import the resources and if you want to refer back to the report later you can save it.

There’s no reason anyone on your team should have to recreate resources from project to project. Whether you develop your own import palette or use one like DWG Resource Importer your team will save production hours.

About Mark Stefanchuk: Mark is a VP and senior consultant with CAD Management Resources, Inc. He divides his time between developing innovative custom software solutions and helping clients navigate complex design automation environments. If you would like to find out how he can assist you with your design technology he can be reached by contacting us at info@cadmanage.com.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: