Hi all,
The following is the code i have written for snap check between the polylines. Any suggesions to improve please let me know.
[CommandMethod("Snapcheck")]
public static void Snapcheck()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Transaction tr = db.TransactionManager.StartTransaction();
Editor ed = doc.Editor;
using (tr)
{
TypedValue[] tpval = new TypedValue[1];
tpval.SetValue(new TypedValue((int)DxfCode.Start, "LWPOLYLINE"), 0);
SelectionFilter fltr = new SelectionFilter(tpval);
PromptSelectionResult sel = ed.SelectAll(fltr);
if (sel.Status == PromptStatus.OK)
{
SelectionSet ss = sel.Value;
foreach (SelectedObject ent in ss)
{
DBObject obj = tr.GetObject(ent.ObjectId, OpenMode.ForRead);
Polyline objpl = obj as Polyline;
if (chksnp(objpl.StartPoint) == "false")
{
errcle(0.5, "Err_Snap", objpl.StartPoint);
}
if (chksnp(objpl.EndPoint) == "false")
{
errcle(0.5, "Err_Snap", objpl.EndPoint);
}
}
}
tr.Commit();
}
}
public static string chksnp(Point3d pnt)
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Transaction tr = db.TransactionManager.StartTransaction();
Editor ed = doc.Editor;
string snp="false";
using (tr)
{
//Point3d pnt = ln.StartPoint;
//double pnt = ln.StartPoint.X;
//double pnt1 = ln.StartPoint.Y;
TypedValue[] tpval = new TypedValue[1];
tpval.SetValue(new TypedValue(10, pnt), 0);
SelectionFilter fltr = new SelectionFilter(tpval);
PromptSelectionResult sel1 = ed.SelectAll(fltr);
if (sel1.Status == PromptStatus.OK)
{
SelectionSet ss1 = sel1.Value;
if (ss1.Count > 1)
{
snp = "true";
}
}
}
return snp;
}
public static void errcle(double rad, string lay, Point3d cnpt)
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
Transaction tr = db.TransactionManager.StartTransaction();
using (tr)
{
BlockTable blktb = tr.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;
BlockTableRecord blktr = tr.GetObject(blktb[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
Circle cr = new Circle();
cr.SetDatabaseDefaults();
cr.Radius = rad;
cr.Layer = lay;
cr.Center = cnpt;
blktr.AppendEntity(cr);
tr.AddNewlyCreatedDBObject(cr, true);
tr.Commit();
}
}