Ad

Saturday, September 15, 2012

Adding a Profile PVI Through the API

The code below shows how to add a PVI to an existing profile. The code has the user select a profile and then the profile view the profile is in. Then using the picked point, or somewhere near it, a PVI is added to the profile.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.Runtime;
using System.Windows.Forms;
using acadApp = Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.Civil.ApplicationServices;
using Autodesk.Civil.DatabaseServices;
using Autodesk.AutoCAD.Geometry;

namespace TestBands
{
public class TestBands
{
[CommandMethod("TestBands")]
public void TestBandsCommand()
{
CivilDocument civDoc = CivilApplication.ActiveDocument;
Database db = acadApp.Application.DocumentManager.MdiActiveDocument.Database;
Editor ed = acadApp.Application.DocumentManager.MdiActiveDocument.Editor;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
ProfileView profView = null;
Point3d ptSelected = new Point3d(0,0,0);
ObjectId profileObjId = SelectProfile(civDoc, "Select profile section to edit: ", out profView, out ptSelected);

Profile profSection = profileObjId.GetObject(OpenMode.ForRead) as Profile;
double dElev = double.MinValue;
double dSta = double.MinValue;

profView.FindStationAndElevationAtXY(ptSelected.X, ptSelected.Y, ref dSta, ref dElev);

profSection.PVIs.AddPVI(dSta, dElev + 10);

ed.Regen();
ed.UpdateScreen();
tr.Commit();
}
}

public static ObjectId SelectProfile(CivilDocument civDoc, string prompt, out ProfileView profView, out Point3d ptSelected)
{
Editor ed = acadApp.Application.DocumentManager.MdiActiveDocument.Editor;
ptSelected = new Point3d(0,0,0);
try
{
PromptEntityOptions pops = new PromptEntityOptions("\n" + prompt);
profView = null;
pops.Message = "\nYou must select a Profile View:";
PromptEntityResult profileRes = ed.GetEntity(pops);

if (profileRes.Status == PromptStatus.OK)
{
// Get the profile view of the profile.
Profile prof = profileRes.ObjectId.GetObject(OpenMode.ForRead) as Profile;
Alignment align = prof.AlignmentId.GetObject(OpenMode.ForRead) as Alignment;
profView = ed.GetEntity(pops).ObjectId.GetObject(OpenMode.ForRead) as ProfileView;
ptSelected = profileRes.PickedPoint;
return profileRes.ObjectId;
}
return ObjectId.Null;


}
catch (Autodesk.AutoCAD.Runtime.Exception exe)
{
ed.WriteMessage("Error creating alignment: " + exe.Message);
}
catch (System.Exception ex)
{
ed.WriteMessage("Error creating alignment: " + ex.Message);
}
profView = null;
return ObjectId.Null;
}
}
}



This example is from a larger project that exports Civil 3D information to ISIS, MIKE, and HEC-RAS. It’s also being used as an example of how the bands do not update when using the API. To update the bands after adding a PVI one has to go into the Profile View Properties. This will cause the bands to reflect changes made using the API.

No comments:

LinkWithin

Blog Widget by LinkWithin

Ad