Hi,
I am Picking an AutoCAD entity Programmatically through c# code . It may be a Line, Polyline or Block reference.
How to get the Picked point in an any AutoCAD entity by using C#? or nearest point to the Picked entity..
Regards,
Viswanathan Solaisamy
Solved! Go to Solution.
Solved by norman.yuan. Go to Solution.
Solved by norman.yuan. Go to Solution.
You should have shown your code that does the picking, so other does not have to guess. But let me assume you use Editor.GetEntity() to do picking. If you pay attention to the return value of this method (class PromptEntityResult), besides "ObjectId", it also includes a property "PickedPoint" - this is what you need.
Just to be aware, this point may not exactly sit on the entity user picks, depending on the size of the mouse' pick box, which is configurarble. If you actually want is the point exactly picked on the entity, you need to calculate the nearest point on the entity from the picked point. Say, if the entity being picked is line/polyline/circle (Curve, in general), you can use Curve.GetClosestPoint([Picked Point], false) to find the point on the curve that corresponding to the picked point.
Hi,
Thanks for your quick response.
From your suggestion understood minimum 2 if condition required to find out which entity returned.
1. For polyline,Line,Circle we can consider as curve.(First 'IF' need to write)
2. For Block reference (second 'IF' need to write)
Here is my code:
if (Selection.Status == PromptStatus.OK)
{
SelectionSet acSSet = Selection.Value;
foreach (SelectedObject acSSObj in acSSet)
{
Entity acadentity = Trans.GetObject(acSSObj.ObjectId, OpenMode.ForWrite) as Entity;
if (TxtIncrementStart.Text != null && TxtIncrementvalue.Text !=null && TxtIncrementFieldname.Text !=null )
{
inc = inc + Convert.ToInt32(TxtIncrementvalue.Text);
string strfieldvalue = Convert.ToString(inc);
UpdateValuetoSingleField(acadentity.Layer, acadentity.ObjectId, strfieldname, strfieldvalue);
}
}
}
Regards,
Viswanathan Solaisamy
Hi,
Full set of code given below:
using (DocumentLock lk = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument())
{
using (Transaction Trans = acaddoc.Database.TransactionManager.StartTransaction())
{
BlockTable acBlkTbl;
acBlkTbl = Trans.GetObject(acaddatabase.BlockTableId,
OpenMode.ForRead) as BlockTable;
BlockTableRecord acBlkTblRec;
acBlkTblRec = Trans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
PromptSelectionOptions Options = new PromptSelectionOptions();
Options.SingleOnly = true;
Options.SinglePickInSpace = true; //Stores the active document
SelectionFilter Filter = new SelectionFilter(new TypedValue[] { new TypedValue(100, "AcDbEntity") });
PromptSelectionResult Selection;
Selection = acaddoc.Editor.GetSelection(Options, Filter);
if (Selection.Status == PromptStatus.OK)
{
SelectionSet acSSet = Selection.Value;
foreach (SelectedObject acSSObj in acSSet)
{
Entity acadentity = Trans.GetObject(acSSObj.ObjectId, OpenMode.ForWrite) as Entity;
acadentity.Highlight();
string entname = RXClass.GetClass(acadentity.GetType()).DxfName;
if (entname == "INSERT") // for "LWPOLYLINE"
{
BlockReference acblockref = (acadentity) as BlockReference;
if (TxtIncrementStart.Text != null && TxtIncrementvalue.Text != null && TxtIncrementFieldname.Text != null)
{
inc = inc + Convert.ToInt32(TxtIncrementvalue.Text);
string strfieldvalue = Convert.ToString(inc);
DBText acText = new DBText();
acText.SetDatabaseDefaults();
acText.Position = acblockref.Position;
acText.Height = 5;
acText.TextString = strfieldvalue;
acBlkTblRec.AppendEntity(acText);
Trans.AddNewlyCreatedDBObject(acText, true);
UpdateValuetoSingleField(acadentity.Layer, acadentity.ObjectId, strfieldname, strfieldvalue);
}
}
}
}
Trans.Commit();
}
}
}
}
acadeditor.Regen();
}
Regards,
Viswanathan Solaisamy
I am confused and not sure what the code you showed has anything to do with your original question. Your original question is about how to get the point on an entity picked (i.e. when an entity is selected by mouse pick, where is the picked point that sits on the entity), right?
Now I am seeing you use GetSelectionSet() , not GetEntity(), so, not every entity selected in the SelectionSet is clicked by the mouse, unless you only select a SINGLE entity. Say, if you select by a window, the mouse is clicked twice, and each click may not fall on any entity, so, no matter how many entities are selected, there is no way to get a PICKED point on each entity, because no entity is actually picked.
Furtheemore, from your code, it seems you are not after picked point at all.
You might want to explain what you want to do in detail and what exactly yur question is.
Hi,
As per your suggestion 'there is no way to get a PICKED point on each entity' accepted this answer.
And also using your first suggestion resolved the issue.
Here is my code:
if (Selection.Status == PromptStatus.OK)
{
SelectionSet acSSet = Selection.Value;
foreach (SelectedObject acSSObj in acSSet)
{
Entity acadentity = Trans.GetObject(acSSObj.ObjectId, OpenMode.ForWrite) as Entity;
acadentity.Highlight();
string entname = RXClass.GetClass(acadentity.GetType()).DxfName;
if (entname == "INSERT")
{
BlockReference acblockref = (acadentity) as BlockReference;
if (TxtIncrementStart.Text != null && TxtIncrementvalue.Text != null && TxtIncrementFieldname.Text != null)
{
inc = inc + Convert.ToInt32(TxtIncrementvalue.Text);
string strfieldvalue = Convert.ToString(inc);
DBText acText = new DBText();
acText.SetDatabaseDefaults();
acText.Position = acblockref.Position;
acText.Height = 5;
acText.TextString = strfieldvalue;
acBlkTblRec.AppendEntity(acText);
Trans.AddNewlyCreatedDBObject(acText, true);
UpdateValuetoSingleField(acadentity.Layer, acadentity.ObjectId, strfieldname, strfieldvalue);
}
}
}
}
Trans.Commit();
}
Thanking you,
Regards,
Viswanathan Solaisamy
Can't find what you're looking for? Ask the community or share your knowledge.