Archive | VB.Net RSS for this section

VBA Custom Configs

by Mark Stefanchuk, cadmanage.com

I’m not really a big fan of increasing the number of MicroStation config variables. Where possible, try to use built in variables. For example, you can use CreateCellElement3

with just the name of the cell as long as the cell library is defined in MS_CELLLIST. You might have something that looks like this in your VB code.

oCell = MainForm.oMSApp.CreateCellElement3(MainForm.uc_o.txtCameraName.Text, Point, True)

This is from a VB.Net example, that’s why the MicroStation Application object is included.

Here’s how I might define MS_CELLLIST for use with photogeoDGN.

CADMANAGE = C:/ProgramData/Cadmanage/PhotogeoDGN/Contents/
MS_ADDINPATH < $(CADMANAGE)Windows/8.11/
MS_CELLLIST < $(CADMANAGE)Resources/*.cel

Sometimes however, it is necessary to identify a special folder or some other resource in your custom program. For example, your program might include a catalog file that provides some engineering data that your program will associate with a cell. You could decide to put this in a resources folder that is always in the install directory. But, a better solution might be to let the end user decide where she wants to save the resource. In this case the resource or file is one that MicroStation doesn’t know anything about, but your program has to be able to find. To fix that we could define the following,

CMRI_CATALOG = $(CADMANAGE)Resources/testcatalog.xml

Put the variable in one of MicroStation’s configuration files, like your UCF. Now, to access the variable from your custom VBA program you need to use ConfigurationVariableValue. Here’s how to do that in VBA.

Sub testConfig()
Debug.Print ActiveWorkspace.ConfigurationVariableValue("CMRI_CATALOG")
End Sub

The example will print the value of the configuration variable in the VBA IDE’s immediate window.

C:\ProgramData\Cadmanage\PhotogeoDGN\Contents\Resources\testcatalog.xml

Use this expansion to open the file, or in this case define your XML document object.

As long as the variable name is different than any other MicroStation config variable, you can use it, but I would recommend  including a prefix. In the previous example, I used CMRI (for CAD Management Resources, Inc). This just helps to avoid variable conflicts.

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 directly at mstefanchuk@cadmanage.com.

photogeoDWG

photoDGN Beta

Advertisements

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.

Multiple Resources (AutoCAD)

by Mark Stefanchuk, CADmanage.com

Seth’s last blog, Resources Resources…, was great! In particular this bit of information really caught my attention.

The Files tab lists the folders that AutoCAD will use to search for resource files (a.k.a. Support Paths), drivers, CUI files, tool palettes, and so on.  There are also user-defined settings such as the  dictionary file used for checking spelling.  Paths are searched in the order that they are listed in the Options dialog box, and if the same file exists in different folders, the first instance found is used.

Ok, so that got me thinking. How do I know if there is more than one file with the same name in my paths folders and is the file I want loaded in the right place? My first thought was to just look in each folder and see if there are duplicates. But, turns out there are a lot of files in these folders. I’m going to need a better way. I could write a vb script to look in each support folder and compare all of the files dumping the results to some output file, but I kind of want to be able to debug my profile folders when I’m in AutoCAD. To do that we will need a different approach.

I decided to build a plug-in. If you’re not familiar with .Net plugins for AutoCAD check out Autodesks “My First Plug-in” tutorials. Really well done. You don’t need the wizard, and you can find the AutoCAD references in the AutoCAD program files folder. There are a lot of on-line tutorials for VB.Net or C# – either programming language can be used to create an AutoCAD plugin. I’ll let you do your own googling to find what you need.

Here are some pointers for getting the support file information from AutoCAD. We will start by just figuring out what our support files are.

Your class will need to reference and Import the following:

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.Interop

In your class you will need a new command to show the support paths.

<CommandMethod("SHOWSP")> _
Public Shared Sub ShowSupPaths()
    Dim acadApp As AcadApplication = Application.AcadApplication
    MsgBox(acadApp.Preferences.Files.SupportPath)
End Sub

Compile this to create a dll that you can netload (just like in the plug-in tutorials). After you netload you can use the command SHOWSP which will display a message box similar to this one.

It’s not very pretty and you can find the same information using the built in OP (options) command. So, what we really want is to get a list of file names from each folder and then compare them to one another to see if they show up more than once. What can we do? The first thing to notice is that the support paths are separated by a semi-colon. That gives us a way to “split” the string into an array of folders. We can then use System.IO to get directory info which we can use to list the file names. Something like this…

Dim sPaths() As String
sPaths = acadApp.Preferences.Files.SupportPath.Split(";")
For Each s As String In sPaths
    Dim di As DirectoryInfo = New DirectoryInfo(s + "\")
    For Each f As FileInfo In di.GetFiles()
        lstFiles.Items.Add(f.Name)
    Next
Next

In this example I have added the file names to a list box. You can loop through the list items to check for duplicates and output to another list box. I took it a step further and built a Palette. In the top panel I added a tree view so that I have a way to step though the folders and files. In the bottom panel I list the files that show up more than once.

I also added a double click feature to open a window and select the file in the folder it resides in so that I have a quick way to compare files. Finally, I decided to add an extension filter so that I can debug just cuix, or lsp files.

We’re looking into posting this on the Autodesk Exchange. So, if you don’t want to dig deeper and try to write it yourself I hope to have this submitted and available in the next few weeks.

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.