[C#] Problem Rotate Circle or Arc with DrawJig

[C#] Problem Rotate Circle or Arc with DrawJig

Anonymous
Not applicable
1,123 Views
1 Reply
Message 1 of 2

[C#] Problem Rotate Circle or Arc with DrawJig

Anonymous
Not applicable

Hi everybody, i wrote this class here because i need to rotate a list of entity but i can't understand why it doesn't work if in the list there's an arc or a circle. The same thing append if i rotate a blockreference and one of the entity of the pointed block is an arc or a circle. thanks everybody!

 

class JigRotateObj : DrawJig
{
Point3d _basePoint;
Point3d oldPoint;
Ray _firstAxis;
Ray _lastAxis;
double firstAngle;
double lastAngle;
double angleToDo;
double angleDone;
public int currentInputValue
{
get;
set;
}

public List<ObjectId> _toRotate
{
get;
set;
}

public JigRotateObj(List<ObjectId> toRotate,Point3d basePoint,Ray firstAxis,Ray lastAxis)
{
angleDone = 0;
angleToDo = 0;
oldPoint = Point3d.Origin;
_toRotate = toRotate;
currentInputValue = 1;
_firstAxis = firstAxis;
_firstAxis.BasePoint = basePoint;
_lastAxis = lastAxis;
_lastAxis.BasePoint = basePoint;
_basePoint = basePoint;
}

protected override bool WorldDraw(Autodesk.AutoCAD.GraphicsInterface.WorldDraw draw)
{
if (currentInputValue == 1)
{
draw.Geometry.Draw(_firstAxis);
}
else
{
draw.Geometry.Draw(_lastAxis);
Transaction trans = AcadUtilities.GetActiveDatabase().TransactionManager.StartTransaction();
Entity app;
foreach (ObjectId obj in _toRotate)
{
app=(Entity)trans.GetObject(obj,OpenMode.ForWrite);
app.TransformBy(Matrix3d.Rotation(angleDone, Vector3d.ZAxis, _basePoint));
app.TransformBy(Matrix3d.Rotation(-angleToDo, Vector3d.ZAxis, _basePoint));
draw.Geometry.Draw(app);
}
angleDone = angleToDo;
trans.Commit();
trans.Dispose();
}
return true;
}

protected override SamplerStatus Sampler(JigPrompts prompts)
{
PromptPointResult ppr;
if (currentInputValue == 1)
{
ppr = prompts.AcquirePoint("Insert first point");
if (ppr.Value.DistanceTo(oldPoint) < 0.01||ppr.Value.DistanceTo(_firstAxis.BasePoint)<0.01)
return SamplerStatus.NoChange;
if (ppr.Status == PromptStatus.OK)
{
_firstAxis.SecondPoint = ppr.Value;
oldPoint = ppr.Value;
firstAngle = new Line(_basePoint, ppr.Value).Angle;
}
}
else
{
ppr = prompts.AcquirePoint("Insert last point");
if (ppr.Value.DistanceTo(oldPoint) < 0.01 || ppr.Value.DistanceTo(_lastAxis.BasePoint) < 0.01)
return SamplerStatus.NoChange;
if (ppr.Status == PromptStatus.OK)
{
_lastAxis.SecondPoint = ppr.Value;
oldPoint = ppr.Value;
lastAngle = new Line(_basePoint, ppr.Value).Angle;
angleToDo = firstAngle - lastAngle;
}
}
return SamplerStatus.OK;
}

0 Likes
1,124 Views
1 Reply
Reply (1)
Message 2 of 2

Balaji_Ram
Alumni
Alumni

Hi,

 

In the code snippet that you have shared, I see that you are using the original entity to transform it from the "WorldDraw" method. This complicates the logic to correctly transform since the entity is already transformed and you only need to consider the incremental angle. I see you have two transformation to handle this situation. Instead you could just use a clone which will always be at the original position as that of the entity. So this simplifies your logic quite a bit. I dont see any other issue why this code would not work with circles. Here is the sample code that I used to test it for jigging with circles and it worked ok. In this code, the cloned entity is jigged :

 

	class JigRotateObj : DrawJig
	{
		Point3d _basePoint;
		Point3d oldPoint;
		Ray _firstAxis;
		Ray _lastAxis;
		double firstAngle;
		double lastAngle;
		double angleToDo;
		double angleDone;

		public int currentInputValue
		{
			get;
			set;
		}

		public List<ObjectId> _toRotate
		{
			get;
			set;
		}

		public JigRotateObj(List<ObjectId> toRotate, Point3d basePoint, Ray firstAxis, Ray lastAxis)
		{
			angleDone = 0;
			angleToDo = 0;
			oldPoint = Point3d.Origin;
			_toRotate = toRotate;
			currentInputValue = 1;
			_firstAxis = firstAxis;
			_firstAxis.BasePoint = basePoint;
			_lastAxis = lastAxis;
			_lastAxis.BasePoint = basePoint;
			_basePoint = basePoint;
		}

		protected override bool WorldDraw(Autodesk.AutoCAD.GraphicsInterface.WorldDraw draw)
		{
			if (currentInputValue == 1)
			{
				draw.Geometry.Draw(_firstAxis);
			}
			else
			{
				draw.Geometry.Draw(_lastAxis);

				Document doc = Application.DocumentManager.MdiActiveDocument;
				Database db = doc.Database;
				using (Transaction tr = db.TransactionManager.StartTransaction())
				{
					foreach (ObjectId obj in _toRotate)
					{
						Entity app = tr.GetObject(obj, OpenMode.ForRead) as Entity;
						Entity clone = app.Clone() as Entity;
						clone.TransformBy(Matrix3d.Rotation(-angleToDo, Vector3d.ZAxis, _basePoint));
						draw.Geometry.Draw(clone);
					}
					angleDone = angleToDo;
					tr.Commit();
				}
			}
			return true;
		}

		protected override SamplerStatus Sampler(JigPrompts prompts)
		{
			PromptPointResult ppr;
			if (currentInputValue == 1)
			{
				ppr = prompts.AcquirePoint("Insert first point");
				if (ppr.Value.DistanceTo(oldPoint) < 0.01 || ppr.Value.DistanceTo(_firstAxis.BasePoint)<0.01)
					return SamplerStatus.NoChange;

				if (ppr.Status == PromptStatus.OK)
				{
					_firstAxis.SecondPoint = ppr.Value;
					oldPoint = ppr.Value;
					firstAngle = new Line(_basePoint, ppr.Value).Angle;
				}
			}
			else

			{
				ppr = prompts.AcquirePoint("Insert last point");
				if (ppr.Value.DistanceTo(oldPoint) < 0.01 || ppr.Value.DistanceTo(_lastAxis.BasePoint) < 0.01)
					return SamplerStatus.NoChange;

				if (ppr.Status == PromptStatus.OK)
				{
					_lastAxis.SecondPoint = ppr.Value;
					oldPoint = ppr.Value;
					lastAngle = new Line(_basePoint, ppr.Value).Angle;
					angleToDo = firstAngle - lastAngle;
				}
			}
			return SamplerStatus.OK;
		}

		[CommandMethod("TestJig")]
		static public void TestJig()
		{
			Document doc = Application.DocumentManager.MdiActiveDocument;
			Database db = doc.Database;
			Editor ed = doc.Editor;

			PromptEntityOptions peo = new PromptEntityOptions("\nSelect a circle:");
			peo.SetRejectMessage("\nMust be a circle...");
			peo.AddAllowedClass(typeof(Circle), false);

			PromptEntityResult per = ed.GetEntity(peo);

			if (per.Status != PromptStatus.OK)
				return;

			List<ObjectId> oids = new List<ObjectId>();
			oids.Add(per.ObjectId);

			Ray firstAxis = new Ray();
			firstAxis.BasePoint = Point3d.Origin;
			firstAxis.SecondPoint = new Point3d(10, 0, 0);

			Ray lastAxis = new Ray();
			lastAxis.BasePoint = Point3d.Origin;
			lastAxis.SecondPoint = new Point3d(0, 10, 0);

			JigRotateObj drawJig = new JigRotateObj(oids, Point3d.Origin, firstAxis, lastAxis);

			drawJig.currentInputValue = 1;
			PromptResult promptResult = Application.DocumentManager.MdiActiveDocument.Editor.Drag(drawJig);

			drawJig.currentInputValue = 2;
			promptResult = Application.DocumentManager.MdiActiveDocument.Editor.Drag(drawJig);
		}
	}

 



Balaji
Developer Technical Services
Autodesk Developer Network