.NET

Reply
Valued Contributor
dynamicscope
Posts: 95
Registered: ‎04-04-2012
Message 1 of 7 (1,426 Views)
Accepted Solution

Create Arc with StartPoint EndPoint Radius?

1426 Views, 6 Replies
02-07-2013 04:09 AM

[Managed ObjectARX 2007 with C#]

 

I am trying to create an arc with start point, end point, and radius.

So that if the arc gets drawn, I going to use the center point.

 

Well, I guess I can use "SendStringToExecute()" or "SendCommand()" and "ObjectAppended" eventhandler to get the arc object.

But this approach seems bit messy, so I am wondering if there is any other simple way.

OK. Try this code (as usually minimum error checking done):

[CommandMethod("ARC2PR", CommandFlags.Modal)]
public static void Arc2PointsRadius()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
PromptPointResult pRes1 = ed.GetPoint("\nFirst point: ");
if (pRes1.Status != PromptStatus.OK) return;
PromptPointResult pRes2 = ed.GetPoint("\nSecond point: ");
if (pRes2.Status != PromptStatus.OK) return;
PromptDistanceOptions prDist =
new PromptDistanceOptions("\nRadius: ");
prDist.BasePoint = pRes2.Value;
prDist.UseBasePoint = true;
PromptDoubleResult rRes = ed.GetDistance(prDist);
if (rRes.Status != PromptStatus.OK) return;
Matrix3d ucsMat = ed.CurrentUserCoordinateSystem;
Vector3d vNormal = ucsMat.CoordinateSystem3d.Zaxis;
Point3d pStart = pRes1.Value.TransformBy(ucsMat); // Start point in WCS
Point3d pEnd = pRes2.Value.TransformBy(ucsMat); // End point in WCS
// Finding center point of arc.
CircularArc3d circ1 = new CircularArc3d(pStart, vNormal, rRes.Value);
CircularArc3d circ2 = new CircularArc3d(pEnd, vNormal, rRes.Value);
Point3d[] pts = circ1.IntersectWith(circ2);
circ1.Dispose(); circ2.Dispose();
if (pts.Length < 2) {
ed.WriteMessage("\nInvalid arguments"); return;
}
Point3d pCenter = pts[0];
Vector3d v1 = pStart - pCenter, v2 = pEnd - pCenter;
if (v1.CrossProduct(v2).DotProduct(vNormal) < 0) {
pCenter = pts[1];
}
CircularArc3d cCirc = new CircularArc3d(pCenter, vNormal, rRes.Value);
double parStart = cCirc.GetParameterOf(pStart);
double parEnd = cCirc.GetParameterOf(pEnd);
double parMid = (parStart + parEnd) * 0.5;
Point3d pMid = cCirc.EvaluatePoint(parMid);
cCirc.Dispose();
CircularArc3d cArc = new CircularArc3d(pStart, pMid, pEnd);
double angle =
cArc.ReferenceVector.AngleOnPlane(new Plane(cArc.Center,cArc.Normal));
Arc arc =
new Arc(cArc.Center, cArc.Normal,
cArc.Radius, cArc.StartAngle + angle, cArc.EndAngle + angle);
cArc.Dispose();
using (Transaction tr = doc.TransactionManager.StartTransaction()) {
BlockTableRecord btr =
tr.GetObject(doc.Database.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
btr.AppendEntity(arc);
tr.AddNewlyCreatedDBObject(arc, true);
tr.Commit();
}
}

 

 

Valued Mentor
gasty1001
Posts: 480
Registered: ‎04-11-2010
Message 2 of 7 (1,417 Views)

Re: Create Arc with StartPoint EndPoint Radius?

02-07-2013 06:25 AM in reply to: dynamicscope

Hi,

 

The problem is that the arc constructor use angles intead points, so you have to calculate the angles from the points, check this link: http://www.theswamp.org/index.php?topic=40382.msg456904#msg456904

 

Gaston Nunez

Moderator
Alexander.Rivilis
Posts: 1,417
Registered: ‎04-09-2008
Message 3 of 7 (1,412 Views)

Re: Create Arc with StartPoint EndPoint Radius?

02-07-2013 07:05 AM in reply to: gasty1001

Creating of arc with startpoint, endpoint and radius can not be unique. Such arcs can be either two or none.

07-02-2013 17-23-46.png


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Valued Contributor
dynamicscope
Posts: 95
Registered: ‎04-04-2012
Message 4 of 7 (1,389 Views)

Re: Create Arc with StartPoint EndPoint Radius?

02-07-2013 05:06 PM in reply to: Alexander.Rivilis

As you would know AutoCAD already provides such feature. (Drawing Arc with Start point + End point + Radius).

So I was wondering if there is any API I can use.

 

It looks like I have to manually calculate angles and stuff.

Hmm.....

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 5 of 7 (1,379 Views)

Re: Create Arc with StartPoint EndPoint Radius?

02-07-2013 11:38 PM in reply to: dynamicscope

Then you can to look at acedCmd and use the same command promts

as per as in ARC command , just an idea

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Moderator
Alexander.Rivilis
Posts: 1,417
Registered: ‎04-09-2008
Message 6 of 7 (1,374 Views)

Re: Create Arc with StartPoint EndPoint Radius?

02-08-2013 01:50 AM in reply to: dynamicscope

OK. Try this code (as usually minimum error checking done):

[CommandMethod("ARC2PR", CommandFlags.Modal)]
public static void Arc2PointsRadius()
{
  Document doc = Application.DocumentManager.MdiActiveDocument;
  Editor ed = doc.Editor;
  PromptPointResult pRes1 = ed.GetPoint("\nFirst point: ");
  if (pRes1.Status != PromptStatus.OK) return;
  PromptPointResult pRes2 = ed.GetPoint("\nSecond point: ");
  if (pRes2.Status != PromptStatus.OK) return;
  PromptDistanceOptions prDist = 
	new PromptDistanceOptions("\nRadius: ");
  prDist.BasePoint = pRes2.Value;
  prDist.UseBasePoint = true;
  PromptDoubleResult rRes = ed.GetDistance(prDist);
  if (rRes.Status != PromptStatus.OK) return;
  Matrix3d ucsMat = ed.CurrentUserCoordinateSystem;
  Vector3d vNormal = ucsMat.CoordinateSystem3d.Zaxis;
  Point3d pStart = pRes1.Value.TransformBy(ucsMat); // Start point in WCS
  Point3d pEnd = pRes2.Value.TransformBy(ucsMat); // End point in WCS
  //  Finding center point of arc. 
  CircularArc3d circ1 = new CircularArc3d(pStart, vNormal, rRes.Value);
  CircularArc3d circ2 = new CircularArc3d(pEnd, vNormal, rRes.Value);
  Point3d[] pts = circ1.IntersectWith(circ2);
  circ1.Dispose(); circ2.Dispose();
  if (pts.Length < 2) {
    ed.WriteMessage("\nInvalid arguments"); return;
  }
  Point3d pCenter = pts[0];
  Vector3d v1 = pStart - pCenter, v2 = pEnd - pCenter;
  if (v1.CrossProduct(v2).DotProduct(vNormal) < 0) {
    pCenter = pts[1];
  }
  CircularArc3d cCirc = new CircularArc3d(pCenter, vNormal, rRes.Value);
  double parStart = cCirc.GetParameterOf(pStart);
  double parEnd   = cCirc.GetParameterOf(pEnd);
  double parMid = (parStart + parEnd) * 0.5;
  Point3d pMid = cCirc.EvaluatePoint(parMid);
  cCirc.Dispose();
  CircularArc3d cArc = new CircularArc3d(pStart, pMid, pEnd);
  double angle = 
	cArc.ReferenceVector.AngleOnPlane(new Plane(cArc.Center,cArc.Normal));
  Arc arc = 
	new Arc(cArc.Center, cArc.Normal, 
		cArc.Radius, cArc.StartAngle + angle,  cArc.EndAngle + angle);
  cArc.Dispose();
  using (Transaction tr = doc.TransactionManager.StartTransaction()) {
    BlockTableRecord btr = 
	tr.GetObject(doc.Database.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
    btr.AppendEntity(arc);
    tr.AddNewlyCreatedDBObject(arc, true);
    tr.Commit();
  }
}

 

 


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Valued Contributor
dynamicscope
Posts: 95
Registered: ‎04-04-2012
Message 7 of 7 (1,324 Views)

Re: Create Arc with StartPoint EndPoint Radius?

02-13-2013 01:36 AM in reply to: Alexander.Rivilis

WOW!!!

Didn't expect the answer code.

It works great for me and it solves my problem.

 

Plus, I learned a lot from your code.

There are so many APIs I can utilize in ObjectARX.

Awesome!!

 

Thank you always~ :smileyhappy:

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.