using Bricscad.ApplicationServices;
using Bricscad.EditorInput;
using Teigha.DatabaseServices;
using Teigha.Geometry;
using Teigha.Runtime;
namespace swamper
{
public class Commands
{
public static void PostToDatabase(Entity entity)
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
btr.AppendEntity(entity);
tr.AddNewlyCreatedDBObject(entity, true);
tr.Commit();
}
}
public static void DrawPipeEcsAxes(Matrix3d pipeEcs)
{
/**
* Note: all points used/aquired are in WCS, this is why we need to
* transform some of them to our pipe
*/
// get a vector from the WCS origin to ECS origin to use in displacement later
var ovec = pipeEcs.CoordinateSystem3d.Origin.GetAsVector();
Point3d origin
= new Point3d
(ovec
.X, ovec
.Y, ovec
.Z); // create xvec and give it length:
Vector3d xvec = pipeEcs.CoordinateSystem3d.Xaxis;
xvec = xvec.MultiplyBy(10); // this is 10mm for me, maybe 10 inches for you??
// NOTE: each of the ECS vectors are directions relative to WCS, we need to move them to the object with TransformBy()
Line xaxis
= new Line
(origin,
new Point3d
(xvec
.X, xvec
.Y, xvec
.Z).TransformBy(Matrix3d
.Displacement(ovec
))); xaxis.ColorIndex = 1;
PostToDatabase(xaxis);
// create yvec and give it length:
Vector3d yvec = pipeEcs.CoordinateSystem3d.Yaxis;
yvec = yvec.MultiplyBy(10);
Line yaxis
= new Line
(origin,
new Point3d
(yvec
.X, yvec
.Y, yvec
.Z).TransformBy(Matrix3d
.Displacement(ovec
))); yaxis.ColorIndex = 100;
PostToDatabase(yaxis);
// create zvec and give it length:
Vector3d zvec = pipeEcs.CoordinateSystem3d.Zaxis;
zvec = zvec.MultiplyBy(10);
Line zaxis
= new Line
(origin,
new Point3d
(zvec
.X, zvec
.Y, zvec
.Z).TransformBy(Matrix3d
.Displacement(ovec
))); zaxis.ColorIndex = 150;
PostToDatabase(zaxis);
}
[CommandMethod("pipetext")]
public static void PipeText()
{
// get the pipe ECS here and pass it to our draw function, we are just using the WCS matrix here:
// drawPipeEcsAxes(myPipe.getEcs());
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
var entResult = ed.GetEntity("Select an entity: ");
if (entResult.Status != PromptStatus.OK) return;
var db = HostApplicationServices.WorkingDatabase;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
var pipe = tr.GetObject(entResult.ObjectId, OpenMode.ForRead, false) as Line; // as 'myPipe' for your task
if(pipe != null)
{
// replace my 'pipe' stuff with the comments at end of line, it just might work ;)
var pipeXaxis = pipe.StartPoint.GetVectorTo(pipe.EndPoint).GetNormal(); // myPipe.Ecs.CoordinateSystem3d.Xaxis
var pipeZaxis = pipe.Normal; // myPipe.Ecs.CoordinateSystem3d.Zaxis
var pipeYaxis = pipeZaxis.CrossProduct(pipeXaxis); // myPipe.Ecs.CoordinateSystem3d.Yaxis
// create the new pipeEcs for use in tranformations:
var pipeEcs = Matrix3d.AlignCoordinateSystem(
// from WCS
Point3d.Origin, Vector3d.XAxis, Vector3d.YAxis, Vector3d.ZAxis,
// to ECS
pipe.StartPoint, pipeXaxis, pipeYaxis, pipeZaxis
);
DrawPipeEcsAxes(pipeEcs);
}
tr.Commit();
}
}
}
}