Ad

Tuesday, June 01, 2010

Convert Block and Text to Civil 3D Points

Once in a while we are presented a task that doesn’t lend itself to the existing commands within Civil 3D. One case that I came across recently was the need to convert blocks and text to Civil 3D points. The points were needed to do a preliminary design and later a survey would be performed to get an adequate surface for design. In this post I’ll create a command that will have the user select the block (for position), select the text (for elevation), create a point and then output the newly created points information to the command line. To accomplish this I’ll be using the free version of the Sincpac.

First thing is I’m going to add a new class to the SincpacC3D 2010 Free Edition solution. Right click on the title in Solution Explorer and choose add new item.

   image

Select the Class template and provide it a name that makes sense.

SNAGHTML3dd372a

We can then open the newly created class and start programming.

image

In order to make it easier to program I usually rely on looking at other code. The easy way to do this is to create a new Vertical or Horizontal Tab. While I don’t state what code I’ve looked at, rest assured that I did use this.

image

Next we need to add the other using statements that will be required by the code and change the class to a public class. If we leave it as a class Civil 3D won’t be able to see the code.

image

Next I’ll add an aeccConn object from the C3DUtilities. The aeccConn object makes it easy to access the current drawings Civil 3D database. It’s a much easier way than outlined in the help files. I’ll use this later on in the code. In order to access it a new instance of it needs to be created. I’ve also add a Command Method for the code.

image

The next step is to get the block where the point I want to create is located at and then make sure getting the block was successful. The AddAllowedClass ensures the user may only select a block and no other types of objects. The SetRejectMessage is necessary to tell the user what type of object may be selected. 

image

If its successful I’ll get the text which contains the elevation information using the same method as the block.

image

Next I’ll get the objects from the drawing’s database to get the required information to create the point, but first I need to get the database and use a transaction to get the objects.

image  From the text I’ll take the text string and convert it to a double value. I’ve enclosed it within a try in case there is other characters in there that aren’t convertible to a double value. If there are other characters additional code would be required to remove them to get to the elevation value only. A search of the internet will find examples on how to parse text.

image

Once the point was created I gave it a generic raw description. Now I’ll output the point’s Northing, Easting and Elevation to the command line. To format the number I’ll use the Formatter code from the Sincpac. This make it easy to convert a number to a string with a set percision, in this case to 2 decimal places.

image

Lastly I’ll end the try, commit the changes to the transaction and set the aeccConn value to null.

image

Now when I run the command I can easily change a block and text into a Civil 3D point. With some additional programming one could use a selection set of blocks and text and associate them based on their location. In my case it wasn’t worth the effort so I spent my time selecting the block and text entities separately. To run the command continuously use the Multiple AutoCAD command to repeat the command over and over again. Alternatively a loop could be added to continue the command until something is triggered, but the multiple command achieves the same affect with less effort on my part.

The completed code formatted poorly:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Interop.Common;
using Autodesk.AECC.Interop.Land;
using Quux.C3DUtilities;
using Quux.AcadUtilities;

namespace Quux.SincpacC3D.ConvertBlocks
{
    public class ConvertBlocks
    {

        private AeccAppConnection aeccConn = null;

        [CommandMethod("SINCPAC-C3D", "ConvBlocks", null, CommandFlags.Modal, null, "SincpacC3D.chm", "SP_ChangeElevation")]
        public void ChangeElevationCommand()
        {
            aeccConn = new AeccAppConnection();
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;

            // Select the block
            PromptEntityOptions pops = new PromptEntityOptions("\nSelect Block: ");
            pops.SetRejectMessage("\nSelection must be a block!");
            pops.AddAllowedClass(typeof(BlockReference), false);
            PromptEntityResult pres;
            pres = ed.GetEntity(pops);

            if (pres.Status == PromptStatus.OK)
            {

                // Select the text
                PromptEntityOptions popsTxt = new PromptEntityOptions("\nSelect Text: ");
                popsTxt.SetRejectMessage("\nSelection must be text!");
                popsTxt.AddAllowedClass(typeof(DBText), false);

                PromptEntityResult presTxt;
                presTxt = ed.GetEntity(popsTxt);

                if (presTxt.Status == PromptStatus.OK)
                {
                    Database db = Application.DocumentManager.MdiActiveDocument.Database;
                    using (Transaction tr = db.TransactionManager.StartTransaction())
                    {
                        try
                        {
                            BlockReference oBlock = tr.GetObject(pres.ObjectId, OpenMode.ForRead, false) as BlockReference;
                            DBText oText = tr.GetObject(presTxt.ObjectId, OpenMode.ForRead, false) as DBText;

                            double dZ;
                            try
                            {
                                dZ = Converter.StringToDistance(oText.TextString);
                                Point3d oPt3D = new Point3d(oBlock.Position.X, oBlock.Position.Y, dZ);
                                AeccPoint oPoint = aeccConn.AeccDb.Points.Add(oPt3D.ToArray());
                                oPoint.RawDescription = "Converted Block and Text";

                                // Send the information to the command line.

                                string sE = Formatter.FormatNumberWithPrecision(oBlock.Position.X, 2);
                                string sN = Formatter.FormatNumberWithPrecision(oBlock.Position.Y, 2);
                                string sZ = Formatter.FormatNumberWithPrecision(dZ, 2);
                                ed.WriteMessage("\nEasting     "+ sE);
                                ed.WriteMessage("\nNorthing    "+ sN+ "    Elevation     "+ sZ);

                            }
                            catch { }
                        }
                        finally
                        {
                            tr.Commit();
                        }
                    }
                }
            }
            aeccConn = null;
        }
    }
}

Friday, May 28, 2010

Sincpac C3D Source Code

Recently Quux Software released the Sincpac C3D Source Code for Civil 3D. If your an amateur programmer like me I’d suggest you download it and check out the features. If your just starting out being an amateur programmer of Civil 3D then this post is for you. In this post I’ll go over how to get the code set up to work and some of the benefits of using the code as a starting point.

The first thing you need is an Integrated Development Environment program. If your just starting out I’d suggest downloading Microsoft Visual Studio Express for c#. If your planning on doing this more than just occasionally I’d get Visual Studio. (If your using Microsoft Visual Studio Express 2010 you may need to change the .NET framework to 3.5 from the default).  The Sincpac is written in c#, which is quite different than the VBA and vb.NET that I usually blog about.

Once you’ve imagedownloaded the source code and Visual Studio you’ll need to open a solution based on what version of Civil 3D you are building for. The Sincpac comes with solutions for 2008, 2009, 2010 & 2011.  Each solution utilizes the same code in the AcadUtilities, C3DUtilities & SincpacC3D folders. This makes it easy to have code for your routines in one place and makes it easy to compile code for each version of Civil 3D.

For this example I’m working with Civil 3D 2010, so I’ll open QuuxFree2010SNAGHTML2a78b8. Once open you may get warning messages. It should be OK to press the OK button. You’ll then get another error message, go ahead and change the option to Load project Normally. You’ll have to press it three times since there is three SNAGHTML2cb71fassociated solutions with the main solution.

Within the Sincpac there is three solutions. The SincpacC3D 2010 Free Edition contains the commands that the user calls.image This, or another solution, is where you’d place your commands or modify the existing ones. The AcadUtilities2010 and C3DUtilities2010 contain code that helps accomplish the tasks in the SincpacC3D 2010 solution.

For instance the  AcadUtilities contains the Formatter Class. This class helps convert doubles to the correct number format. For instance you can convert a radian value to the correct angle format using the AngleToString.

SNAGHTML330686

In order to compile the code, most likely, the references need to point to the correct location for all three solutions. Thankfully you only have to do this step the first time out.  I find the easiest way is to add a reference folder location. I’ve chosen to use the Civil 3D folder, but it is recommended to use the ObjectARX files, downloadable from the ADN site. Download it and then point the reference folder to that location instead of what I did in the video.

Now you can build the solution, choose to build the entire solution. Now open Civil 3D and use Netload to load the dll. The dll may be found here for a Build Solution:

\FreeEditionV2\SincpacC3D\bin\Release2010

and here for a Debug Release:

\FreeEditionV2\SincpacC3D\bin\Debug2010

The download page has some information how you can have the dll loaded when Civil 3D starts so you don’t have to netload it each time you want to debug a program.

The commands that come with the Sincpac code is: BRKPT, CHELEV, FREEPT, HIDEBND, LABLAY, MOVEPOINTS, NEXTPT, PT2FEATURE, PT2PROFILE, ROTATEMARKERS, ROTATEPOINTS, SELPTGROUP, SGOBJECT, SGRD, SPHELP, SPSETTINGS, STAKEFEATURES, STAOFF, USEDPT, & ZOOMPT. You can find more information about the commands on the Sincpac site.

In the next post I’ll start to add a custom command, using this code as a starting point, to create Civil 3D points from blocks and text.

***Disclaimer: I have received access to the full version of the Sincpac without having to pay for it, which may need to be disclosed due to certain laws of the United States.

Monday, May 17, 2010

AU Voting is Here

From the AU Site:

“Here is your chance to influence which classes will be offered at AU this year. Help us select from the more than 1800 topics submitted by the international AU community. It only takes a few minutes to vote for those sessions that you want to see offered. Voting ends Friday, May 21.  Vote now!”

So if you are planning on going to AU or watching on AU Virtual now is your chance to influence the classes chosen. I’d suggest you help out Earl get to the class he wants to attend.

Thursday, May 13, 2010

c# Changing References

Sometimes you come across some code that’s been built on another version of Civil 3D then what you want. In order for it to work you need to change the references. Depending on your development environment you can modify the references the way that’s done in this post, if not the steps are slightly different. For this post I’m changing the references in the Quux’s Free version of the SincPac 3D (available soon), from Civil 3D 2010 to 2011. 

The first step is to add a Reference Path. To do this go to the Project Properties, Reference Path Tab and then add the folder.

You can also add references by right clicking in the Solution Explorer and selecting them from the list or browsing to the location. First you need to delete the reference from the previous or later version.

Now all you should need to do is check for any version API issues and build the code.

Wednesday, May 12, 2010

Adding Layouts to Sheet Set Manager

When using the sample sheet set manager code that ships with Autocad, ObjectARX or Civil 3D make sure to save the drawing first. If you don't you spend a bunch of time trying to figure out what you are doing wrong.
The AutoCAD .NET Developer's Guide provides a way to save a drawing.

LinkWithin

Blog Widget by LinkWithin

Ad