PDA

View Full Version : Joining Polylines in AutoCAD with C#



Smirzor
2016-10-20, 08:10 AM
I am trying to select all polylines of a specific layer and then join them with the normal autocad _JOIN command. For some reason i just cant get it to work.

The selection set is properly found as i could loop through it and change the color of each (just did that for testing purpose)
But for the join command it seems to only select the first polyline of the layer.
What am i missing/doing wrong here?

(i can also not use
ed.Command("_.PEDIT", "_Multiple", ss, "", "_Join", 0.0, ""); instead as i have a mixture of 3d polylines and 2d polylines wildly floating around in the modelspace)

Thanks in advance for any help!


[CommandMethod("JOINPOLY",
CommandFlags.UsePickSet |
CommandFlags.Redraw |
CommandFlags.Modal)]
public void SelectAllPolylineByLayer(string layerName)
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;

using (Transaction tr = db.TransactionManager.StartTransaction())
{
try
{
// create the typevalue (criteria what should be selected)
TypedValue[] tvs = new TypedValue[] {
new TypedValue(Convert.ToInt32(DxfCode.Operator), "<and"),
new TypedValue(Convert.ToInt32(DxfCode.LayerName), layerName),
new TypedValue(Convert.ToInt32(DxfCode.Operator), "<or"),
new TypedValue(Convert.ToInt32(DxfCode.Start), "POLYLINE"),
new TypedValue(Convert.ToInt32(DxfCode.Start), "LWPOLYLINE"),
new TypedValue(Convert.ToInt32(DxfCode.Start), "POLYLINE2D"),
new TypedValue(Convert.ToInt32(DxfCode.Start), "POLYLINE3d"),
new TypedValue(Convert.ToInt32(DxfCode.Operator), "or>"),
new TypedValue(Convert.ToInt32(DxfCode.Operator), "and>")
};

// create a selectionfilter out of our created typevalue
SelectionFilter oSf = new SelectionFilter(tvs);
PromptSelectionResult selRes = ed.SelectAll(oSf);

// if there is a problem with the promtselection stop here
if (selRes.Status != PromptStatus.OK)
{
ed.WriteMessage("\nError in getting the selectAll");
return;
}

SelectionSet ss = selRes.Value;

ed.Command("_JOIN", ss, "");
tr.Commit();
}
//Catch the error and write the errormessage
catch (System.Exception ex)
{
ed.WriteMessage(Convert.ToString(ex));
}
}
}

FisherMan
2016-10-30, 04:32 PM
Are you sure that its possible to join 3d-polylines with the command pedit?
I mean not: Only 2d-polylines with the same z-level, connected by one point with identical coordinate.