public static Point3d PolarPoint(Point3d basepoint, double angle, double distance)
{
// credits to Tony Tanzillo
return new Point3d(
basepoint.X + (distance * Math.Cos(angle)),
basepoint.Y + (distance * Math.Sin(angle)),
basepoint.Z);
}
[CommandMethod("PARC")]
public void addArcs()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
Transaction tr = doc.TransactionManager.StartTransaction();
using (tr)
{
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
// prompt for selecting polyline
PromptEntityOptions peo = new PromptEntityOptions("\nSelect a polyline: >>");
peo.SetRejectMessage("\nSelect a polyline only >>");
peo.AddAllowedClass(typeof(Polyline), false);
peo.AllowObjectOnLockedLayer = true;
PromptEntityResult res;
res = ed.GetEntity(peo);
if (res.Status != PromptStatus.OK) return;
Entity ent = (Entity)tr.GetObject(res.ObjectId, OpenMode.ForRead);
if (ent == null) return;
Polyline pline = (Polyline)ent as Polyline;
if (pline == null) return;
// get the number of segments
int segments = pline.NumberOfVertices - 1;
for (int i = 0; i < segments; i++)
{
if (pline.GetSegmentType(i) == SegmentType.Arc)
continue;
// get arc segment on polyline
LineSegment2d lseg = pline.GetLineSegment2dAt(i);
// get center
Point2d center = lseg.EvaluatePoint(0.5);
// create an Arc
Point2d p1 = lseg.StartPoint;
Point2d p2 = lseg.EndPoint;
double ang = lseg.Direction.Angle;
Point3d mp = new Point3d((p1.X + p2.X) / 2, (p1.Y + p2.Y) / 2,pline.Elevation).TransformBy(Matrix3d.Identity);
// get half of hord
double cat = lseg.Length / 2.0;
// you have soecify radius of an arc here:
double rad = lseg.Length * 1.25;// <-- dummy radius for test
// get next catet
double bcat = Math.Sqrt(rad * rad - cat * cat);
Point3d cp = PolarPoint(mp, ang + Math.PI / 2, bcat);
Plane plan = new Plane(Point3d.Origin, Vector3d.ZAxis);
Point3d sp=new Point3d(p1.X,p1.Y,pline.Elevation);
Point3d ep = new Point3d(p2.X, p2.Y, pline.Elevation);
// create arc using center radius and both angles, count clockwise from start line to end
Arc arc = new Arc(cp, rad, cp.GetVectorTo(sp).AngleOnPlane(plan), cp.GetVectorTo(ep).AngleOnPlane(plan));
// add to current space and transaction
btr.AppendEntity(arc);
tr.AddNewlyCreatedDBObject(arc, true);
}
tr.Commit();
}
}
[CommandMethod("FPL")]
public void filletPline()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = HostApplicationServices.WorkingDatabase;
try
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
Point3dCollection pts = new Point3dCollection();
Point3d pt = new Point3d(0, 0, 0);
pts.Add(pt);
pt = new Point3d(100, 0, 0);
pts.Add(pt);
pt = new Point3d(100, 50, 0);
pts.Add(pt);
pt = new Point3d(0, 50, 0);
pts.Add(pt);
Polyline pol = new Polyline(pts.Count);
Matrix3d ucs = ed.CurrentUserCoordinateSystem;
Point3d origin = new Point3d(0, 0, 0);
Vector3d normal = new Vector3d(0, 0, 1);
normal = normal.TransformBy(ucs);
Plane plane = new Plane(origin, normal);
int j = 0;
foreach (Point3d pnt in pts)
{
Point3d transformedPt = pnt.TransformBy(ucs);
pol.AddVertexAt(pol.NumberOfVertices, plane.ParameterOf(transformedPt), 0, 0, 0);
pol.SetEndWidthAt(j, 2);
pol.SetStartWidthAt(j, 2);
j++;
}
pol.SetBulgeAt(1, Math.PI / 4);
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
btr.AppendEntity(pol);
tr.AddNewlyCreatedDBObject(pol, true);
tr.Commit();
}
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
ed.WriteMessage(ex.StackTrace);
}
}
[DllImport("acad.exe", BestFitMapping = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Auto)]
private static extern int acedCmd(System.IntPtr cmd);
[CommandMethod("FILET")]
public static void TestFillet()
{
Database db = HostApplicationServices.WorkingDatabase;
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Transaction tr = db.TransactionManager.StartTransaction();
using (tr)
{
try
{
// Prompt for the polyline to revcloud
PromptEntityOptions peo = new PromptEntityOptions("\nSelect a polyline:");
peo.SetRejectMessage("\nSelect polyline only");
peo.AddAllowedClass(typeof(Polyline), true);
PromptEntityResult per = ed.GetEntity(peo);
if (per.Status != PromptStatus.OK)
{
return;
}
ObjectId fid = per.ObjectId;
DBObject obj1 = tr.GetObject(fid, OpenMode.ForWrite);
Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("CMDECHO", 0);
Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("FILLETRAD", 50.0);
ResultBuffer buf = new ResultBuffer();
buf.Add(new TypedValue(5005, "_FILLET"));
buf.Add(new TypedValue(5005, "_P"));
buf.Add(new TypedValue(5006, fid));
acedCmd(buf.UnmanagedObject);
buf.Dispose();
Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("CMDECHO", 1);
tr.Commit();
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
ed.WriteMessage(ex.Message + "\n" + ex.StackTrace);
}
}
}
Private Sub spoly_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles spoly.Click
Dim doc As Document = DocumentManager.MdiActiveDocument
Dim db As Database = doc.Database
Dim ed As Editor = doc.Editor
Dim rad As Double = radius.Text
Dim peo As New PromptEntityOptions(vbLf & "Select a polyline: ")
peo.SetRejectMessage("Not a polyline.")
peo.AddAllowedClass(GetType(Polyline), True)
Dim per As PromptEntityResult = ed.GetEntity(peo)
If per.Status <> PromptStatus.OK Then
Return
End If
Dim kutevi As New DoubleCollection
Dim tocke As New Point3dCollection
Dim duljine As New DoubleCollection
Using tr As Transaction = db.TransactionManager.StartTransaction()
Dim pline As Polyline = DirectCast(tr.GetObject(per.ObjectId, OpenMode.ForWrite), Polyline)
FilletAll(pline, rad)
For i As Integer = 0 To pline.NumberOfVertices - 1
If pline.GetSegmentType(i) = SegmentType.Arc Then
kutevi.Add(pline.GetArcSegment2dAt(i).EndAngle - pline.GetArcSegment2dAt(i).StartAngle)
Dim j As Integer = 1
Dim l12d As New LineSegment2d(pline.GetArcSegment2dAt(i).StartPoint, pline.GetArcSegment2dAt(i).EndPoint)
Dim vecv2d As Vector2d = (l12d.MidPoint - pline.GetArcSegment2dAt(i).Center)
Dim kut2 As Double = vecv2d.Angle
Dim x As Double = rad / Math.Cos((pline.GetArcSegment2dAt(i).EndAngle - pline.GetArcSegment2dAt(i).StartAngle) * 0.5)
Dim k As Point3d = PolarPoint(pline.GetArcSegmentAt(i).Center, kut2, x)
'tocke.Add(k)
Dim vl1 As New Line(pline.GetArcSegmentAt(i).StartPoint, k)
Dim VL2 As New Line(pline.GetArcSegmentAt(i).EndPoint, k)
tocke.Add(VL2.EndPoint)
MsgBox("x=" & k.X.ToString & " y=" & k.Y.ToString)
draw(HostApplicationServices.WorkingDatabase, vl1)
draw(HostApplicationServices.WorkingDatabase, VL2)
End If
Next
tr.Commit()
End Using
End Sub