Line segments connected by CAD at the beginning and end are connected to multiline

Source: Internet
Author: User
Tags polyline

/// <Summary>
/// Line segments connected at the beginning and end are connected to multiline
/// V1.0 by weltionchen @ 2011.02.17
/// Implementation principle:
/// 1. Select all line segments on the drawing
/// 2. Select the first line of the selected set as the starting line segment and search for the connected line segment in two directions.
/// 3. The selectcrossingwindow method of the editor is used for searching. An optional set is created through the endpoint of the line segment.
/// Under normal circumstances, one or two line segments will be selected (this program will not deal with the intersection of the three line segments at the moment), remove itself, and obtain the connected line segments
/// 4. The processed line segment is no longer the starting line segment and is removed from the set.
/// 4. Search by recursive loop until the end.
/// 5. Delete the original line segment and create a multiline
/// 6. process all line segments cyclically
/// </Summary>
[Commandmethod ("tt5")]
Public void jionlinestopline ()
{
// Select all line segments on the drawing
Editor ED = Autodesk. AutoCAD. applicationservices. application. documentmanager. mdiactivedocument. Editor;
Selectionfilter Sf = new selectionfilter (New typedvalue [] {New typedvalue (0, "line ")});
Promptselectionresult selectlinesresult = ed. selectall (SF );
If (selectlinesresult. status! = Promptstatus. OK)
Return;
// Set of line segments to be processed
List <objectid> lineobjectids = new list <objectid> (selectlinesresult. value. getobjectids ());
Using (transaction TR = hostapplicationservices. workingdatabase. transactionmanager. starttransaction ())
{
Database DB = hostapplicationservices. workingdatabase;
Blocktablerecord currentspace = tr. GetObject (db. currentspaceid, openmode. forwrite) as blocktablerecord;
While (true)
{
// Select the first line of the selected set as the starting line
Objectid currentlineid = lineobjectids [0];
// The processed line segments are no longer used as the starting line segments and are excluded from the set.
Lineobjectids. removeat (0 );
Line currentline = tr. GetObject (currentlineid, openmode. forwrite) as line;
// A set of multi-line vertices, composed of the endpoints of the straight line segments connected to each other. The initial value is the endpoint of the starting line segment.
List <point3d> plinepoints = new list <point3d> {currentline. startpoint, currentline. endpoint };
// Each line segment has two directions, from the start point to the end point.
Jionlinestopline (ref lineobjectids, TR, ref plinepoints, currentlineid, currentlineid );
// Flip Point Set
Plinepoints. Reverse ();
// Search from the end point to the start point
Jionlinestopline (ref lineobjectids, TR, ref plinepoints, currentlineid, currentlineid );
// This program is used to convert the connected straight line segments into multi-line segments, so the isolated straight line segments are not processed.
If (plinepoints. Count> 2)
{
// Create a multiline
Autodesk. AutoCAD. databaseservices. polyline resultpline = new Autodesk. AutoCAD. databaseservices. polyline ();
For (INT I = 0; I <plinepoints. Count-1; I ++)
{
Resultpline. addvertexat (I, new point2d (plinepoints. X, plinepoints. Y), 0, 0, 0 );
}
If (plinepoints [0] = plinepoints [plinepoints. Count-1])
{
Resultpline. Closed = true;
}
Else
{
Resultpline. addvertexat (plinepoints. count-1, new point2d (plinepoints [plinepoints. count-1]. x, plinepoints [plinepoints. count-1]. y), 0, 0, 0 );
}
Resultpline. layer = currentline. layer;
Resultpline. linetype = currentline. linetype;
Resultpline. linetypescale = currentline. linetypescale;
Currentspace. appendentity (resultpline );
Tr. addnewlycreateddbobject (resultpline, true );
// Delete the start line segment
Currentline. Erase ();
}
// After processing is completed, jump out of the loop
If (lineobjectids. Count = 0)
Break;
}
Tr. Commit ();
}
}
/// <Summary>
/// Link a line segment to a multi-line recursive loop part
/// V1.0 by weltionchen @ 2011.02.17
/// </Summary>
/// <Param name = "lineobjectids"> objectid set of a line segment </param>
/// <Param name = "TR"> transaction </param>
/// <Param name = "plinepoints"> multi-line vertex coordinate, which is also a set of end point coordinates of each line segment </param>
/// <Param name = "currentlineid"> objectid of the current line segment </param>
Void jionlinestopline (Ref List <objectid> lineobjectids, transaction TR, Ref List <point3d> plinepoints, objectid currentlineid, objectid startlineid)
{
// Extract the endpoint
Point3d lastpoint = plinepoints [plinepoints. Count-1];
Editor ED = Autodesk. AutoCAD. applicationservices. application. documentmanager. mdiactivedocument. Editor;
Selectionfilter Sf = new selectionfilter (New typedvalue [] {New typedvalue (0, "line ")});
// Create an optional set by clicking
Promptselectionresult selectlinesresult = ed. selectcrossingwindow (lastpoint, lastpoint, SF );
If (selectlinesresult. Status = promptstatus. OK)
{
List <objectid> selectedlinesid = new list <objectid> (selectlinesresult. value. getobjectids ());
// Remove itself
Selectedlinesid. Remove (currentlineid );
// Process connected line segments
If (selectedlinesid. Count = 1)
{
Objectid selectedlineid = selectedlinesid [0];
// The processed line segments are no longer used as the starting line segments and are excluded from the set.
If (selectedlineid! = Startlineid)
{
Lineobjectids. Remove (selectedlineid );
Line selectedline = tr. GetObject (selectedlineid, openmode. forwrite) as line;
// Add Vertex
If (selectedline. startpoint = lastpoint)
{
Plinepoints. Add (selectedline. endpoint );
}
Else
{
Plinepoints. Add (selectedline. startpoint );
}
// Recursive search
Jionlinestopline (ref lineobjectids, TR, ref plinepoints, selectedlineid, startlineid );
// Delete the intermediate Line Segment
Selectedline. Erase ();
}
}
}
}
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.