As you can see, this Class inserts a Text with the actual length of a curve
at the cursorposition.
All works without any problems in wcs. The text rotates like it should. When
i try it in a ucs, the text sometimes rotates a little bit wrong, as i said,
sometimes and not allways.
I have no idea what i can do, any ideas?
code
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#region Using directives
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Globalization;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
#endregion
[assembly: CommandClass(typeof(RSNNAcadApp.ExtendedCommands.TestText))]
namespace RSNNAcadApp.ExtendedCommands
{
class TestText
{
private double ScaleFaktor = 2.5;
private Curve CurveObj;
[CommandMethod("TestTextEinfüge")]
public void TestTextInsert()
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-us");
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
int Luprec = Application.DocumentManager.MdiActiveDocument.Database.Luprec;
try
{
#region User Input
bool RightObject = false;
//Selection of a Curve
PromptEntityOptions CurveOptions = new PromptEntityOptions("Objekt wählen,
an dem augerichtet werden soll");
PromptEntityResult CurveResult = null;
while (!RightObject)
{
bool PromptCancel = false;
do
{
CurveResult = ed.GetEntity(CurveOptions);
} while (CurveResult.Status == PromptStatus.Error);
if (CurveResult.Status == PromptStatus.Cancel)
return;
else if (CurveResult.Status == PromptStatus.OK)
{
ObjectId entid = CurveResult.ObjectId;
Entity tmpEnt = null;
using (Transaction myT2 = db.TransactionManager.StartTransaction())
{
tmpEnt = (Entity)myT2.GetObject(entid, OpenMode.ForRead);
}
CurveObj = tmpEnt as Curve;
if (CurveObj != null)
{
string ScaleFaktorPrompt = ScaleFaktor.ToString("F" + Luprec.ToString());
ed.WriteMessage(String.Format("\nFaktor = {0}", ScaleFaktorPrompt));
//highlight the entity
CurveObj.Highlight();
bool Flip = false;
while (true)
{
//Ask for StartPoint
#region Get InsertPoint
InsertBlockJig jig = new
InsertBlockJig(CurveObj.GetClosestPointTo(CurveResult.PickedPoint, false),
CurveObj, ScaleFaktor, Flip);
PromptPointResult InsertResult = (PromptPointResult)ed.Drag(jig);
if (InsertResult.Status == PromptStatus.Cancel | InsertResult.Status ==
PromptStatus.Error)
{
PromptCancel = true;
break;
}
else if (InsertResult.Status == PromptStatus.OK)
{
DBText TextObj = (DBText)jig.GetEntity();
using (Transaction myT = db.TransactionManager.StartTransaction())
{
BlockTableRecord btr = (BlockTableRecord)myT.GetObject(CurveObj.OwnerId,
OpenMode.ForWrite, false);
btr.AppendEntity(TextObj);
myT.AddNewlyCreatedDBObject(TextObj, true);
myT.Commit();
}
}
else if (InsertResult.Status == PromptStatus.Keyword)
{
}
else
{
PromptCancel = true;
break;
}
#endregion
}
CurveObj.Unhighlight();
if (PromptCancel)
break;
RightObject = false;
}
else
{
CurveOptions.Message = "Falsches Objekt!\nObjekt wählen, das gemessen werden
soll";
}
}
}
#endregion
}
finally
{
}
}
class InsertBlockJig : EntityJig
{
Point3d mStartPt, acquiredPoint;
Curve mCurve;
bool mFlip;
double mScale;
Vector3d xAxis;
Matrix3d TextMatrix = new Matrix3d();
CoordinateSystem3d UCS = new CoordinateSystem3d();
Plane UCSPlane = new Plane();
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
double i;
//DynamicDimensionDataCollection m_dims;
public InsertBlockJig(Point3d Start, Curve Kurve, double Scale, bool Flip)
: base(new DBText())
{
mCurve = Kurve;
mStartPt = Start;
mScale = Scale;
mFlip = Flip;
i = 0;
((DBText)Entity).Height = mScale;
((DBText)Entity).VerticalMode = TextVerticalMode.TextBottom;
((DBText)Entity).HorizontalMode = TextHorizontalMode.TextCenter;
Update();
/*
m_dims = new DynamicDimensionDataCollection();
Dimension dim1 = new AlignedDimension();
dim1.SetDatabaseDefaults();
m_dims.Add(new DynamicDimensionData(dim1, true, true));
*/
}
protected override SamplerStatus Sampler(JigPrompts prompts)
{
JigPromptPointOptions jigOpts = new JigPromptPointOptions();
jigOpts.UserInputControls = (UserInputControls.Accept3dCoordinates |
UserInputControls.InitialBlankTerminatesInput |
UserInputControls.NullResponseAccepted);
jigOpts.Message = "\nEinfügepunkt angeben oder neues";
//jigOpts.Keywords.Add("Objekt");
//jigOpts.Keywords.Add("Faktor");
//jigOpts.Keywords.Add("Spiegeln");
//jigOpts.Keywords.Default = "Objekt";
PromptPointResult res = prompts.AcquirePoint(jigOpts);
if (res.Status == PromptStatus.Cancel | res.Status == PromptStatus.Keyword)
return SamplerStatus.Cancel;
acquiredPoint = res.Value;
if (mStartPt != acquiredPoint)
{
mStartPt = acquiredPoint;
}
else
return SamplerStatus.NoChange;
return SamplerStatus.OK;
}
protected override bool Update()
{
try
{
Point3d CurvePoint = mCurve.GetClosestPointTo(mStartPt, false);
((DBText)Entity).TextString = mCurve.GetDistAtPoint(CurvePoint).ToString();
xAxis = mCurve.GetFirstDerivative(CurvePoint).GetNormal();
((DBText)Entity).Position = mStartPt;
if (i == 0)
{
Matrix3d UCSMatrix = ed.CurrentUserCoordinateSystem;
UCS = UCSMatrix.CoordinateSystem3d;
TextMatrix = Matrix3d.AlignCoordinateSystem(mStartPt, Vector3d.XAxis,
Vector3d.YAxis, Vector3d.ZAxis, mStartPt, UCS.Xaxis, UCS.Yaxis, UCS.Zaxis);
((DBText)Entity).TransformBy(TextMatrix);
++i;
UCSPlane = new Plane(mStartPt, UCS.Xaxis, UCS.Yaxis);
}
ed.WriteMessage("\nWinkel: " +
UCSPlane.GetCoordinateSystem().Xaxis.ToString());
double Angle = xAxis.AngleOnPlane(UCSPlane);
ed.WriteMessage("\nWinkel: " + Angle);
((DBText)Entity).Rotation = Angle;
}
catch (System.Exception)
{
return false;
}
return true;
}
protected override DynamicDimensionDataCollection
GetDynamicDimensionData(double dimScale)
{
return base.GetDynamicDimensionData(dimScale);
}
protected override void
OnDimensionValueChanged(DynamicDimensionChangedEventArgs e)
{
base.OnDimensionValueChanged(e);
}
void UpdateDimensions()
{
/*
DBText MyLine = (DBText)Entity;
AlignedDimension dim = (AlignedDimension)m_dims[0].Dimension;
dim.XLine1Point = MyLine.Position;
dim.XLine2Point = MyLine.Position;
dim.DimLinePoint = MyLine.Position;
*/
}
public Entity GetEntity()
{
((DBText)Entity).AlignmentPoint = mStartPt;
return Entity;
}
}
}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~