.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Problem with Jig

3 REPLIES 3
Reply
Message 1 of 4
Anonymous
598 Views, 3 Replies

Problem with Jig

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;

}

}

}

}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3 REPLIES 3
Message 2 of 4
Mick Duprez
in reply to: Anonymous

Is your text insert point xformed to wcs? this can give you incorrect results when working in different ucs's.
Message 3 of 4
Anonymous
in reply to: Anonymous

Try tis code:
[code]
class InsertBlockJig : EntityJig
{

Point3d mStartPt, acquiredPoint, CurvePoint;
Curve mCurve;
bool mFlip;
double mScale;
double UCSAngle;
Vector3d xAxis;
Matrix3d TextMatrix = new Matrix3d();
CoordinateSystem3d UCS = new CoordinateSystem3d();
Plane UCSPlane = new Plane();
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;

double i;

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;
((DBText)Entity).SetDatabaseDefaults(HostApplicationServices.WorkingDatabase);
((DBText)Entity).TextStyle = HostApplicationServices.WorkingDatabase.Textstyle;
Update();
}

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";

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
{
CurvePoint = mCurve.GetClosestPointTo(mStartPt, ed.GetCurrentView().ViewDirection,false);
double parm = mCurve.GetParameterAtPoint(CurvePoint);
((DBText)Entity).TextString = mCurve.GetDistanceAtParameter(parm).ToString();

xAxis = mCurve.GetFirstDerivative(CurvePoint).GetNormal();

((DBText)Entity).Position = mStartPt;

if (i == 0)
{

Matrix3d UCSMatrix = ed.CurrentUserCoordinateSystem;
UCS = UCSMatrix.CoordinateSystem3d;
TextMatrix = Matrix3d.AlignCoordinateSystem(CurvePoint, Vector3d.XAxis, Vector3d.YAxis, Vector3d.ZAxis,
UCS.Origin, UCS.Xaxis, UCS.Yaxis, UCS.Zaxis);
((DBText)Entity).TransformBy(TextMatrix);
UCSAngle = ((DBText)Entity).Rotation;
++i;
UCSPlane = new Plane(CurvePoint, UCS.Xaxis, UCS.Yaxis);
}
ed.WriteMessage("\nWinkel: " + UCSPlane.GetCoordinateSystem().Xaxis.ToString());
Vector3d v = xAxis.ProjectTo(UCSPlane.Normal, ed.GetCurrentView().ViewDirection);
double Angle = v.AngleOnPlane(UCSPlane);
ed.WriteMessage("\nWinkel: " + Angle);
ed.WriteMessage("\nUCSAngle: " + UCSAngle);
((DBText)Entity).Rotation =
((Angle + UCSAngle) > Math.PI * 2.0) ? (Angle + UCSAngle - Math.PI * 2.0) : (Angle + UCSAngle);
((DBText)Entity).AlignmentPoint = CurvePoint;
((DBText)Entity).AdjustAlignment(HostApplicationServices.WorkingDatabase);
}
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()
{
}

public Entity GetEntity()
{
((DBText)Entity).AlignmentPoint = CurvePoint;
return Entity;
}
}
[/code]
Message 4 of 4
Anonymous
in reply to: Anonymous

Thank you very much, Alexander!
I've learned so many thing from your example that i was searching for for so
long.
It works like i wanted, no, it is much better.

--
Roland Feletic
Ingenieurbuero A. Pauser Ges.m.b.H.
http://www.pauser.at

hp workstation xw4200, 3GB
quadro fx1400
AutoCAD 2007, 3DSMax 8.0 SP 2

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost