.NET

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

Create Arc with StartPoint EndPoint Radius?

1599 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();
}
}

 

 

Distinguished Mentor
gasty1001
Posts: 577
Registered: ‎04-11-2010
Message 2 of 7 (1,590 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,446
Registered: ‎04-09-2008
Message 3 of 7 (1,585 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,562 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,552 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,446
Registered: ‎04-09-2008
Message 6 of 7 (1,547 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,497 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:

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are You Going To Be @ AU 2014? Feel free to drop by our AU topic post and share your plans, plug a class that you're teaching, or simply check out who else from the community might be in attendance. Ohh and don't forgot to stop by the Autodesk Help | Learn | Collaborate booths in the Exhibit Hall and meet our community team if you get a chance!