Showing posts with label VB.NET. Show all posts
Showing posts with label VB.NET. Show all posts

Tuesday, April 2, 2013

ObjectARX 2014 and AutoCAD 2014 .NET Training

Autodesk has posted the ObjectARX 2014 and AutoCAD 2014 .NET Training materials to AutoCAD developer page under Learning section.
Visit for downloading the Training materials.

The following new or changed features are now exposed through the API:

  • Connected Desktop - Design Feed
  • Geolocation
  • Point Cloud

There are also quite a few other useful news for developers. For example for .NET these are some examples: MLeader.Scale Property that gets or sets the scale for the MLeader or RegionAreaProperties with properties to get area, centroid point, extents, moments of inertia, perimeter, principal moments, product of inertia and radii of gyration.

See also AutoCAD 2014 API for more.

Monday, April 23, 2012

No source available in Visual Studio debugging AutoCAD .NET code

AutoCAD 2011 through AutoCAD 2013 is being in progress migrating to fiberless operation. See Wikipedia for more about Fibers.

This affects ARX application using acedCommand (replaced with acedCommandS and acedCommandC), acedCmd (replaced with acedCmdS and acedCmdC) as well as ARX or .NET application using AcApDocManager functions to create, close or activate documents. If P/Invoke has been used to access acedCommand in .NET API, it will not work.  Finally LISP *error* handlers using the (command) function. In most cases you simply have to replace (command) from within an *error* handler with calls to the new (command-s) function. Worth knowing is that (command-s) is significantly faster than (command). A limitation with (command-s) is that “pause” tokens cannot be used and the expression must represent complete commands..

AutoCAD 2011 introduced two variables FIBERWORLD and NEXTFIBERWORLD to help programmers to test and migrate code.
FIBERWORLD = 1 if Fibers are being used.
Change NEXTFIBERWORLD to 0 to disable fibers and 1 to enable fibers. Close and relaunch AutoCAD, or close all the current documents in AutoCAD and then open/new one.

If breakpoints in custom forms being opened using ShowModalDialog or ShowDialog cannot be hit when debugging .NET addins in Visual Studio 2010 the workaround is to set AutoCAD to run in fiberless operation (FIBERWORLD = 0). When you try to debug you may see this message in Visual Studio: No Source Available. No symbols are loaded for any call stack frame. The source code cannot be displayed.

The problem that you may have with FIBERWORLD = 0 is that the Ribbon is not working, you may get strange crashes and have other issues. If you cannot type any commands you may need to disable and enable the command line using the application menu and search for command. If you need to work on a specific drawing it can help to load it automatically from within the Visual Studio debugger as the open command may break the command functionality.

When ready remember to set back AutoCAD to run in fiber mode again.

Monday, April 9, 2012

VBA Download for AutoCAD 2013

VBA (Microsoft Visual Basic for Applications) is still available in AutoCAD 2013 but it is not included with the installation media. Find the VBA for AutoCAD downloads here at There downloads are found also for AutoCAD 2010, AutoCAD 2011, AutoCAD 2012 in both 32-bit and 64-bit.

If you have existing VBA this can be good news but don’t get started to develop anything new in VBA for AutoCAD as we don’t know if this will be the last release with VBA or not. Consider to convert the code to .NET (C# or VB.NET) and if you like help with it please feel free to contact us.

Sunday, March 11, 2012

Extract zip files in VB.NET

Here is one way to extract zip files. The example is done in VB.NET using the Shell COM.

Declarations needed:

Imports System.IO
' Add COM reference "Microsoft Shell Controls And Automation"
Imports Shell32

Code snippet:

Dim ZipFile As String = "D:\temp\"
Dim ZipDir As String = "D:\temp\files\"
Dim Shell As Shell32.IShellDispatch2
Dim ShellFolder As Shell32.Folder

If File.Exists(ZipFile) Then
    Shell = CType(CreateObject("Shell.Application"), IShellDispatch2)

    If Not Directory.Exists(ZipDir) Then Directory.CreateDirectory(ZipDir)
    ShellFolder = Shell.NameSpace(ZipDir)
    If ShellFolder IsNot Nothing Then
    End If
End If

One downside with this approach is that a dialog box can show up while extracting the zip file.

Other ways to Zip and Unzip in C#, VB, any .NET language is using DotNetZip (recommended) or SharpZipLib as well as SevenZipSharp for 7z (7-zip). There is also the GZipStream Class that comes with .NET Framework but only works with .gz files. System.IO.Packaging.ZipPackage is available in .NET Framework 3 and newer but is not straightforward to use and not recommended.

Friday, June 10, 2011

.NET code problem on Windows Server 2003 solved

I spent several hours hunting down a problem a customer had with JTB FlexReport on Windows Server 2003 SP2. The solution was in the end a single row. Things worked fine on Windows XP, Windows Vista, Windows Server 2008, Windows Server 2008 SP2  and Windows 7, but for some reason it failed on Windows Server 2003.

In the code I narrowed down that this call failed but I could not figure out why:

Database db = DatabaseFactory.CreateDatabase();

I got this error using Microsoft Enterprise Library 2.0:

Object reference not set to an instance of an object.

I was wondering if it had to do with the Enterprise Library version but I got this error using Microsoft Enterprise Library 5.0:

Activation error occured while trying to get instance of type Database, key ""

I then wondered if the problem was that I had one .exe made by VB.NET and a DLL made by C#, or that it was running as a Windows Service, or that some code had been upgraded all the way from VB6 etc.

Eventually I was able to scale away a copy of my program to just a few rows and still had the problem. Then I created a similar program from scratch and got it running. I could then start to compare the two file by file using DiffMerge and eventually I was able to find that in app.manifest I had to add the following:

<assemblyIdentity version="" name=""/>

just before <trustInfo

Now that I found the solution I looked up more details and found that reading any configuration settings from the applications .config file is not working. I also found that it actually is due to a bug in Windows Server 2003. Calls to System.Configuration.ConfigurationManager.AppSettings return null. In Windows Server 2003, if the application has the following SxS manifest, then CLR will not use the application config file even if it exists. This bug does not exist in Windows XP, Windows Vista or Windows 7. Application config file is not used if the application has certain SxS manifest and Lesson Learned: Missing language attribute in ‘assemblyidentity’ element can cause SxS (SideBySide) application loading failure on Windows XP and Server 2003

Sunday, June 5, 2011

How to rename a registry key with .NET

Let’s say I have a registry key named “HKEY_CURRENT_USER\SOFTWARE\JTB World 1” and I want to rename it to “HKEY_CURRENT_USER\SOFTWARE\JTB World 2”. This is easily done in the Registry Editor (regedit.exe).

But neither Microsoft.Win32.Registry class nor .NET Framework My.Computer.Registry provide rename functionality.

One solution is using REG.EXE, copying the key and all subkeys and then delete the old key. No need to recursively copy keys as would be needed if using the Registry class.

Here is a VB.NET example that easily can be converted to for example C#, BAT/CMD/VBS (VBScript). No error checking added. WindowStyle is used to avoid seeing Windows command window showing.

Dim startInfo As New ProcessStartInfo("REG.EXE")
startInfo.WindowStyle = ProcessWindowStyle.Hidden
startInfo.Arguments = "COPY """ & OldKey & """ """ & NewKey & """ /s"
Dim myProcess As Process = Process.Start(startInfo)
startInfo.Arguments = "DELETE """ & OldKey & """ /f"

Monday, February 7, 2011

Error 1001. The specified service has been marked for deletion

I’ve created an installer using a Visual Studio 2008 deployment project where I install and uninstall a Windows service using a custom action and run into this problem when reinstalling the program.

Error 1001. The specified service has been marked for deletion

Installation Incomplete: The installer interrupted before JTB FlexReport Core could be installed. You need to restart the installer to try again. Click "Close" to exit.

Restarting Windows helps but I wanted to avoid that.

I tried to use sc delete but that didn’t work. I found a tip that services.msc should be closed but I did not have it running. I tried to manually tweak the registry HKLM\SYSTEM\CurrentControlSet\Services but it did not help either.

It took a while to identify the problem but the solution was easy. Close or exit from Sysinternals Process Explorer and run the setup again. For some reason Process Explorer is locking the service process and causing this problem.

Wednesday, December 29, 2010

How to start a new process de-elevated after installation

I was trying to launch an application at the end of an installation I’ve created with Visual Studio. My problem was that the installation is running with elevated privileges as administrator (UAC) and I want the application to run as a normal user.

One problem with the application that was launched by the installer was that when reading and writing to the registry it ended up at HKEY_USERS\.DEFAULT instead of HKEY_CURRENT_USER. I noticed that the process was run by NT AUTHORITY\SYSTEM instead of the expected COMPUTERNAME\USERNAME. This will also cause problems if you want to open a webpage after the installation and there is no current Web Browser running.

I knew how to elevate the process using the verb runas.

p.StartInfo.UseShellExecute = true; p.StartInfo.Verb = "runas";

But I could not find any way to use the verb to de-elevate. I searched for a solution online but could only find convoluted ways like using CreateProcessAsUserW, Code Injection in the shell process or using the task scheduler to register a task to run as the currently logged on desktop user.

I found this post and tried using the User Account Control Helpers to no avail for some reason.

Process p = UacHelpers.UserAccountControl.CreateProcessAsStandardUser("notepad.exe", "");

Eventually I came up with this simple C# code thinking it might do the trick. It actually runs the new program with reduced permissions but not as the intended user. This could be useful in other situations though.

Process p = new Process(); p.StartInfo.FileName = "runas.exe"; p.StartInfo.WindowStyle = ProcessWindowStyle.Normal; p.StartInfo.Arguments = "/trustlevel:0x20000 notepad.exe"; p.Start();

Unfortunately it didn’t help me here.

I then turned back to the UacHelpers source code and commented out these rows because I noticed that the process was not really elevated from the start so I did not want the simple way to start the process.

//if (!IsCurrentProcessElevated) //{ // return Process::Start(exePath, arguments); //}

Using this trick it was possible to have an application like my setup that runs with administrator privileges (thanks to UAC) to start another process with standard user privileges and as the expected user.

For more details on the subject see also Aaron Margosis’ WebLog.

Please comment if this helps you or if you have any related suggestions on better approach.

Thursday, October 28, 2010

Asynchrony Await and the Async CTP

More Asynchrony in programming to look forward to. And for users it means more responsive and quicker applications.

Async CTP introduces a new keyword await, which indicates that the caller would like control to return when an asynchronous method call has completed. And the Async modifier does not create additional threads.

public async Task<int> SumPageSizesAsync(IList<Uri> uris) { int total = 0; foreach (var uri in uris) { statusText.Text = string.Format("Found {0} bytes ...", total); var data = await new WebClient().DownloadDataTaskAsync(uri); total += data.Length; } statusText.Text = string.Format("Found {0} bytes total", total); return total; }

Public Async Function SumPageSizesAsync(uris As IList(Of Uri)) As Task(Of Integer) Dim total As Integer = 0 For Each uri In uris statusText.Text = String.Format("Found {0} bytes ...", total) Dim data = Await New WebClient().DownloadDataTaskAsync(uri) total += data.Length Next statusText.Text = String.Format("Found {0} bytes total", total) Return total End Function

Making Asynchronous Programming Easy

Asynchrony in C# 5, Part One

Announcing the Async CTP for Visual Basic (and also Iterators!)

The async CTP homepage:

Tuesday, May 18, 2010

Access Database Engine Redistributable 2010

This is great news as it makes it possible to make add-in programs for example for AutoCAD 64-bit to access MS Access databases. Among JTB World’s software I plan on implementing this for ACAD_db and ACA_db. If there is a need for it JTB FlexReport could be supported as a native 64-bit application and still have the option to support MS Access databases, not sure if there is any pressing need for it though.

Formerly known as Office Connectivity Component, Access Database Engine 2010 is now available in both 32-bit and 64-bit. It is available in 9 languages. You can download it here.

This download will install a set of components that can be used by non-Microsoft Office applications to read data from and write data to Office 2010 system files such as Microsoft Access 2010 (mdb and accdb) files and Microsoft Excel 2010 (xls, xlsx, and xlsb) files.

  • If you are an application developer using OLEDB, set the Provider argument of the ConnectionString property to “Microsoft.ACE.OLEDB.14.0”
  • If you are connecting to Microsoft Office Excel data, add “Excel 14.0” to the Extended Properties of the OLEDB connection string.
  • If you are application developer using ODBC to connect to Microsoft Office Access data, set the Connection String to “Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=path to mdb/accdb file”
  • If you are application developer using ODBC to connect to Microsoft Office Excel data, set the Connection String to “Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=path to xls/xlsx/xlsm/xlsb file”

Tuesday, May 11, 2010

Autodesk possible cross-product scripting language/IDE

This post has best before date of May 30th, 2010.

Autodesk is researching candidates for a possible cross-product scripting language/IDE. What is your preferred language?

  • Javascript/ECMAScript
  • Python
  • Ruby
  • Lua
  • VBA/VBScript

If you want to make your voice heard on this question and others take the survey for the product you work with.

AutoCAD® Civil 3D®
AutoCAD® Map 3D®
Autodesk Inventor®
Autodesk Navisworks®

Via Through the Interface: API wishlist surveys for Autodesk products

Tuesday, December 22, 2009

Run-time error '429': ActiveX component can't create object solution for VBA 64-bit

Code like this will result in error in AutoCAD Architecture 2010 64-bit:
Set SchedApp = New AecScheduleApplication

Or this in AutoCAD 2010 64-bit:
Set SSM = New AcSmSheetSetMgr

One method that seems to work is to move the code from within a sub to the declaration like this:
Private SchedApp As New AecScheduleApplication

Another method is to replace it using GetInterfaceObject like this:
Set SchedApp = ThisDrawing.Application.GetInterfaceObject("AecX.AecScheduleApplication.6.0")

The reason is that VBA runs as an out-of-process client in 64-bit applications.

Unfortunately this also means that Sheet Set Manager API cannot be used in 64-bit applications because it is a COM server that cannot be called from another process as opposed to an ActiveX server.

Another 64-bit related problem is in AutoCAD Architecture 2009/2010 and AutoCAD MEP 2009/2010. In Tools>References the AutoCAD tlb shows up but not for the verticals. The same problem is also in .NET if you look at the COM tab when adding references.

The solution for this is a fix in the registry that look like this:

@="C:\\Program Files\\Common Files\\Autodesk Shared\\AecXBase60.tlb"

It needs to be done for each tlb you want to use. There is a win64 key but the win32 key is missing so it needs to be added.

Here is also a Microsoft KB describing similar problems with Office.

Monday, October 19, 2009

Learn how to convert VBA code into VB.NET for AutoCAD

Visit Through the Interface for details on the free webcast.

The session will be taking place at 8am Pacific/10am Eastern/4pm in the UK/5pm in Central Europe, on October 20, 2009. If you cannot make it the webcast will be recorded.

If you need help to convert or migrate old VBA code to VB.NET or C#, JTB World can help with this service. You’re welcome to contact us.

Tuesday, July 28, 2009

AutoCAD .NET Developer's Guide

This is really great news! I have reviewed a lot of the guide and can testify that it will be useful for many.
This guide provides information on how to use the AutoCAD .NET API with Microsoft Visual Studio and the programming languages VB.NET and C#. Information specific to developing applications using Microsoft Visual Studio can be found under the topics “Getting Started with Microsoft Visual Studio” and “Develop Applications with Microsoft Visual Studio.”
Programmers developing with the .NET Framework using a development environment other than Microsoft Visual Studio can skip these two chapters. However, all of the example code in this guide is presented in VB.NET and C#.
You can access the AutoCAD .NET Developer’s Guide online:


Getting Started with Microsoft Visual Studio

Basics of the AutoCAD .NET API

Control the AutoCAD Environment

Create and Edit AutoCAD Entities

Dimensions and Tolerances

Work in Three-Dimensional Space

Define Layouts and Plot

Use Events

Develop Applications with VB.NET and C#

VBA/VB to VB.NET and C# Comparison

VBA to VB.NET and C# Comparison

AutoCAD .NET Developer's Guide

Here is also some great screencasts from Microsoft.

Sunday, June 7, 2009

CodeRush Xpress free Visual Studio 2008 add-in

CodeRush Xpress is a FREE Visual Studio 2008 add-in containing 60+ refactorings, 7 editing features, and full support for C# and Visual Basic.

Download here

CodeRush Xpress includes the following features.

  • Duplicate Line
  • Highlight All References
  • Increase or Reduce Selection
  • Smart Clipboard Operations
  • Generate from Using (TDD)
  • Quick Navigation Window
  • Quick File Navigation

In addition, you receive the following refactorings.

Add Parameter
Create Overload
Create With Statement
Encapsulate Field
Encapsulate Field (read only)
Extract Interface
Extract Method
Extract Property
Extract XML Literal to Resource
Flatten Conditional
Introduce Constant
Introduce Constant (local)
Introduce Local
Introduce Local (replace all)
Inline Temp
Inline With Statement
Make Explicit
Make Explicit (and Name Anonymous Type)
Make Implicit
Method to Property
Move Declaration Near Reference
Move Initialization To Declaration
Move Type to File
Name Anonymous Type
Property to Method(s)
Remove Assignments to Parameter
Reorder Parameters
Remove Unused Parameter
Replace Temp with Query
Reverse Conditional
Simplify Expression
Split Initialization from Declaration
Split Temporary Variable
Widen Scope
Widen Scope (promote constant)
Widen Scope (promote to field)
Add Block Delimiters
Combine Conditionals
Compress to Lambda Expression
Compress to Ternary Expression
Convert to Auto-implemented Property
Convert to Initializer
Create Backing Store
Decompose Initializer
Decompose Parameter
Expand Lambda Expression
Expand Ternary Expression
Extract Method
Flatten Conditional
Inline Delegate
Inline Temp
Introduce Local
Make Explicit
Make Implicit
Move Type to File
Name Anonymous Method
Name Anonymous Type
Reverse Conditional
Split Conditional
Use String.Format
Use StringBuilder

Via The Visual Basic Team

Wednesday, May 27, 2009

ProSteel 3D customization using COM with AutoLISP and VBA

I have done coding using the ProSteel API and it is pretty extensive and flexible. I made improvements to the logic used to add position numbers on the steel structure objects as well as creating a two way sync of object properties between AutoCAD and a database. I also created other customizations to automate the workflow further both for working with the 3D model as well as the 2D generation. Another thing I did was to manipulate the ProSteel model so it worked better to use the information in Navisworks.

Either ps3d_com.arx (PSComWrapper Type Library) or ProStructuresLoader.arx are used and it works fine to develop with either AutoLISP or VBA. (VB.NET or C# should be possible to use)

Unfortunately there are not many resources about usage of the ProSteel COM interface other than the included documentation but the other day I run into this site that has some examples posted.

ProSteel was created by Kiwi Software GmbH and later acquired by Bentley and is an add-on running on the AutoCAD platform.

If you need any customization done for ProSteel (or AutoPLANT Structural) feel free to contact me.

Previous blog post talking about ProSteel 3D.

Wednesday, April 29, 2009

Learn how to migrate from VBA to .NET for AutoCAD

VBA is going away from AutoCAD in a coming release but ActiveX that is the API will remain so you can use the same API you are familiar with within .NET using COM Interop. So if you’re familiar with the ActiveX Object Model you get get up and running in .NET quicker.

In the four examples in this Autodesk Developer Network Network DevTV Visual Basic Express is used and you learn how to prepare code to use late binding and then export from VBA via VB6 format and end up with VB.NET code. There is even a great free macro that makes the VB6 project without requiring VB6.

AutoCAD VBA to .NET Migration Basics
download (140 Mb)
supporting files (1.72 Mb)

  • Example 1 is showing manual migration using VB6
  • Example 2 is showing the migration using the migration macro
  • Example 3 is dealing with user forms and problems with late binding and why it is good to change Object to something more specific using early binding
  • Example 4 is for referencing other type libraries than the AutoCAD ones. The example shows object referencing Excel and Word

Via Through the Interface

See also the posts More details on VBA in AutoCAD 2010 products, AutoCAD 2010 VBA and VSTA future and No VBA installed with AutoCAD 2010

Friday, April 24, 2009

ShowModalDialog API changes in AutoCAD 2010

I got this error in some .NET code I updated to get running for AutoCAD 2010.

Error    1    Reference required to assembly 'PresentationCore, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35' containing the implemented interface 'System.Windows.Interop.IWin32Window'. Add one to your project.    D:\VB.NET\MainProject\Mainclass.vb    15    74    MainProject

I found that I needed to change this code showing a modal dialog box.

Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(Application.MainWindow, DialogObj)

to look like this as well as adding the PresentationCore reference.

Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(Application.MainWindow.Handle, DialogObj, False)

Monday, April 13, 2009

TreeTrim for Visual Studio project cleanup

TreeTrim can be useful if you want to cleanup your projects.

It's a tool that strips out debug files and folders in your source code tree and also zips and emails amongst other things.

You can either run it from the command line or from Windows Explorer. The setup program intalls context menu commands when right-clicking on folders in Windows Explorer.

See also the TreeTrim FAQ

In one project I had it cleaned out 30 MB. I tested on a Visual Studio 2008 VB.NET project on Windows 7 and it worked great.

Via Steve Dunn’s blogpost New tool: TreeTrim.

Saturday, April 11, 2009

MetaDraw review

I tested MetaDraw from Bennet-Tec some time ago. Unfortunately I have not had time to do a in depth test or use it in some real development so this is quite brief.

MetaDraw is available as either ActiveX or .NET WinForms component for creating drawings, diagrams, image annotations. MetaDraw has optional DXF support (import and export) making it possible to display and convert 2D CAD drawings for example from AutoCAD. I was able to save a complex drawing in AutoCAD 2010 to DXF 2010 format and view and edit it with MetaDraw.

If you have the need to display images in raster or vector format, convert between formats, support zoom and pan, edit and add labels, annotations and the like MetaDraw is worth looking at.

With MetaDraw you can include basic CAD functionality in your software like editing objects and even have links between object making it easy to make diagrams. The API is extensive so you can do quite a lot of things with it. Several code examples are available and together with the documentation you should be up and running quickly.

If you have Internet Explorer (ActiveX required) you can try these demos directly in the web browser. Online Drawing, HotSpots Demo, Puzzle Sample and MD Map Demo.

The MetaDraw product have been around many years continually being improved upon. The latest version when writing this was released in March 2009. I tested using Visual Studio 2008 and Windows Vista as well as Windows 7.

This animated GIF is created when I try one of the sample applications that shows the functionality to link objects.

MetaDraw .NET 2.0 is a 32-bit DLL so it can not be used in 64-bit applications even though it can be used in 64 bit environments if the application is 32-bit.

Disclaimer. I was offered a free license with no strings attached.

Some of the latest blog posts

Subscribe to RSS headline updates from:
Powered by FeedBurner