A cadastral application can host the control as the map sheet. A parcel is a closed contour, the parcel number is a text entity, and a building inside the parcel is an inner contour. The control draws the sheet, returns the entity under the pointer and the area of the contour, and stores a new boundary back into the drawing.
The map sheet
ReadDXF and ReadDWG
ReadDXF and ReadDWG open the cadastral sheet and display it. Boundaries, buildings, parcel numbers and annotations are entities on layers. The DXF property gives access to those layers, so the application can show the boundaries and hide the rest. ZoomCenter frames the sheet. WriteDXF and WriteDWG save the updated sheet.
dxfReaderNETControl1.ReadDXF(@"C:\cadastre\sheet.dxf");
dxfReaderNETControl1.ZoomCenter(); dxfReaderNETControl1.DXF.Layers["BUILDINGS"].IsVisible = false;
The parcel under the pointer
GetEntity
CoordsToWorld converts the click into a point of the sheet. GetEntity returns the nearest entity: the boundary line, the building or the parcel number. When the entity is a Text, its Value is the parcel identifier drawn on the map. GetEntities returns every entity inside a rectangle, which is the extract of one part of the sheet. HighlightEntityOnHover shows which entity will be picked.
Vector2 point = dxfReaderNETControl1.CoordsToWorld(e.X, e.Y);
EntityObject entity = dxfReaderNETControl1.GetEntity(point);
if (entity != null && entity.Type == EntityType.Text) { string parcelNumber = ((Text)entity).Value; }
Area of the parcel
FindClosedAreaData
MathHelper.FindClosedAreaData reads the entities that form the parcel. It returns the outer perimeter, the outer area, the total length and area of the inner contours, and the number of those contours. A building or a courtyard inside the parcel is an inner contour. The land area is the outer area minus the inner area. DXF.ToFormattedUnit writes the result with the precision of the drawing. ShowFilledAreas paints the parcel so the user can see which contour was measured.
double externalLength, externalArea, internalLength, internalArea;
int innerContours; bool found = MathHelper.FindClosedAreaData( parcelEntities, out externalLength, out externalArea, out internalLength, out internalArea, out innerContours);
double landArea = externalArea - internalArea; dxfReaderNETControl1.ShowFilledAreas(parcelEntities);
A new boundary
AddLine, AddPolyline and AddText
A subdivision is new geometry on the sheet. ObjectSnap locks the pointer to a corner (Endpoint) or to the crossing of two boundaries (Intersection). ShowRubberBandLine draws the line while the user moves the pointer. AddLine stores it. A closed parcel outline is an AddPolyline with the closed flag. AddText writes the new parcel number, and AddAlignedDimension writes the length of a boundary.
dxfReaderNETControl1.AddLine(startPoint, endPoint);
dxfReaderNETControl1.AddPolyline(vertices, true); dxfReaderNETControl1.AddText("124", labelPoint, labelPoint, 2.5);
dxfReaderNETControl1.AddAlignedDimension(startPoint, endPoint, 4);
The corner coordinates
CoordsToWorld
The X and Y returned by CoordsToWorld are the coordinates of the sheet. With object snap on an endpoint, the point is the corner of the parcel. WorldToCoords draws a marker at a coordinate that the application already knows.
Vector2 corner = dxfReaderNETControl1.CoordsToWorld(e.X, e.Y);
string x = dxfReaderNETControl1.DXF.ToFormattedUnit(corner.X);
string y = dxfReaderNETControl1.DXF.ToFormattedUnit(corner.Y);
The extract
Plot and Image
Plot prints the current view of the sheet. PlotPreview draws the same view into a PrintPreviewControl, and ShowPlotSettings opens the plot dialog. The Image property is the bitmap of the extract. WriteDXF saves the sheet, including the new boundary and the new parcel number.
dxfReaderNETControl1.PlotRendering = PlotRenderingType.Monochrome;
dxfReaderNETControl1.Plot();