Hi, I've just started trying to learn to code for Civil3D and have written a simple command to draw a different coloured circle on a surfec depending on the elevation at that point.
My code builds with no errors and I load it into C3D with NETLOAD with no errors but the command doesn't work and doesn't show up in the list of available commands. The last few things I've tried to write have done the same and I've no idea why.
Any assistance would be greatly appreciated; I'm pullijng my hair out!
Thanks in advance, code is below (not sure if this is the best way to add it) .......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Colors;
using Autodesk.Civil;
using Autodesk.Civil.Runtime;
using Autodesk.Civil.DatabaseServices;
using Autodesk.Civil.DatabaseServices.Styles;
using Autodesk.Civil.ApplicationServices;
using Autodesk.Civil.Settings;
namespace ElevationColours
{
publicclassCommand : IExtensionApplication
{
#region IExtensionApplication Members
publicvoid Initialize()
{
thrownew System.Exception("The method or operation is not implemented.");
}
publicvoid Terminate()
{
thrownew System.Exception("The method or operation is not implemented.");
}
#endregion
[CommandMethod("ELEVCOL")]
publicvoid ElevCol()
{
Document acdoc = Application.DocumentManager.MdiActiveDocument;
Database acdb = acdoc.Database;
CivilDocument civdoc = CivilApplication.ActiveDocument;
Editor ed = acdoc.Editor;
PromptPointOptions ppo = newPromptPointOptions("");
PromptPointResult ppr; ;
ppo.Message = ("Pick a point......");
ppr = ed.GetPoint(ppo);
Point3d point = ppr.Value;
double elev;
using (Transaction acTrans = acdoc.TransactionManager.StartTransaction())
{
BlockTable blktbl = acTrans.GetObject(acdb.BlockTableId, OpenMode.ForRead) asBlockTable;
BlockTableRecord blktblrec = acTrans.GetObject(blktbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) asBlockTableRecord;
ObjectIdCollection surfids = civdoc.GetSurfaceIds();
ObjectId surfid = surfids[0];
Autodesk.Civil.DatabaseServices.Surface surf = acTrans.GetObject(surfid, OpenMode.ForRead) as Autodesk.Civil.DatabaseServices.Surface;
elev = surf.FindElevationAtXY(point.X, point.Y);
int color = (int)elev;
Circle circ = newCircle();
circ.SetDatabaseDefaults();
circ.Center = point;
circ.Radius = 10;
circ.ColorIndex = color;
blktblrec.AppendEntity(circ);
acTrans.AddNewlyCreatedDBObject(circ, true);
acTrans.Commit();
}
}
}
}
Solved! Go to Solution.
Solved by Jeff_M. Go to Solution.
What version of C3D and .NET are you targeting?
@attilathedunne wrote:
....I've no idea why.
Any assistance would be greatly appreciated; I'm pullijng my hair out!
Thanks in advance, code is below (not sure if this is the best way to add it) .......
namespace ElevationColours
{
publicclassCommand : IExtensionApplication
{
#region IExtensionApplication Members
publicvoid Initialize()
{
thrownew System.Exception("The method or operation is not implemented."); <<<<<<<<<< You are forcing an exception so it never fully loads, have it do either something or nothing.
}
publicvoid Terminate()
{
thrownew System.Exception("The method or operation is not implemented."); <<<<<<<<<< You are forcing an exception, have it do either something or nothing.
}
#endregion
I should add that you don't need to use the IExtensionApplication unless you have code that needs to run in the Initialize process. Most of the sample & free apps I've posted here do not use it. So just this would work for your commands:
///all code prior to here OK namespace ElevationColours { public class CustomCommands [CommandMethod("ELEVCOL")] public void ElevCol() //everything after here should be ok (not checked)