Message 1 of 9
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello again.
I am trying to hatch the newly created region. I will post some of my tries as comments. Would be very grateful if somebody points me into right diretion, at the moment feels like I am too stuck to get it moving again. The final region might have holes in it.
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using System;
using System.Collections.Generic;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Colors;
public class Commands
{
[CommandMethod("TurvaAla")]
public void OffsetLines()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTableRecord currentSpace = tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForWrite) as BlockTableRecord;
// Filter for lines on the "Alusraamistik" layer
TypedValue[] filterList = new TypedValue[]
{
new TypedValue((int)DxfCode.LayerName, "Alusraamistik"),
new TypedValue((int)DxfCode.Start, "LINE")
};
SelectionFilter selFilter = new SelectionFilter(filterList);
PromptSelectionResult selectionResult = ed.SelectAll(selFilter);
if (selectionResult.Status != PromptStatus.OK)
{
ed.WriteMessage("\nNo lines found on the 'Alusraamistik' layer.");
return;
}
SelectionSet selectionSet = selectionResult.Value;
// Specify the distance for parallel lines
double offsetDistance = 2.0;
// Lists to store unique points
List<Point3d> uniquePoints = new List<Point3d>();
foreach (ObjectId lineId in selectionSet.GetObjectIds())
{
Line originalLine = tr.GetObject(lineId, OpenMode.ForRead) as Line;
if (originalLine != null)
{
// Get the angle of the original line
double angle = originalLine.Angle;
// Calculate the offset vectors
Vector3d offsetVector1 = new Vector3d(Math.Cos(angle + Math.PI / 2), Math.Sin(angle + Math.PI / 2), 0) * offsetDistance;
Vector3d offsetVector2 = new Vector3d(Math.Cos(angle - Math.PI / 2), Math.Sin(angle - Math.PI / 2), 0) * offsetDistance;
// Calculate new start and end points for the parallel lines
Point3d startPoint1 = originalLine.StartPoint.Add(offsetVector1);
Point3d endPoint1 = originalLine.EndPoint.Add(offsetVector1);
Point3d startPoint2 = originalLine.StartPoint.Add(offsetVector2);
Point3d endPoint2 = originalLine.EndPoint.Add(offsetVector2);
// Create new polyline in the "Turvaala" layer
Polyline polyline = new Polyline();
polyline.AddVertexAt(0, new Point2d(startPoint1.X, startPoint1.Y), 0, 0, 0);
polyline.AddVertexAt(1, new Point2d(endPoint1.X, endPoint1.Y), 0, 0, 0);
polyline.AddVertexAt(2, new Point2d(endPoint2.X, endPoint2.Y), 0, 0, 0);
polyline.AddVertexAt(3, new Point2d(startPoint2.X, startPoint2.Y), 0, 0, 0);
polyline.Closed = true;
polyline.Layer = "Turvaala";
currentSpace.AppendEntity(polyline);
tr.AddNewlyCreatedDBObject(polyline, true);
// Add unique points to the list
AddUniquePoint(uniquePoints, originalLine.StartPoint);
AddUniquePoint(uniquePoints, originalLine.EndPoint);
}
}
// Create circles for each unique point
foreach (Point3d uniquePoint in uniquePoints)
{
Circle circle = new Circle(uniquePoint, Vector3d.ZAxis, 2.0);
circle.Layer = "Turvaala";
currentSpace.AppendEntity(circle);
tr.AddNewlyCreatedDBObject(circle, true);
}
tr.Commit();
// Call the ConvertToRegion method
ConvertToRegion();
// Delete the original polylines and circles
DeleteCreatedEntities();
}
}
// Helper method to add unique points to the list
private void AddUniquePoint(List<Point3d> uniquePoints, Point3d point)
{
if (!uniquePoints.Contains(point))
{
uniquePoints.Add(point);
}
}
private Region ConvertToRegion()
{
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
// Start a transaction
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Open the Block table for read
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
// Open the Block table record Model space for write
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
// Filter for polylines and circles on the "Turvaala" layer
TypedValue[] filterList = new TypedValue[]
{
new TypedValue((int)DxfCode.LayerName, "Turvaala"),
new TypedValue((int)DxfCode.Start, "LWPOLYLINE,CIRCLE")
};
SelectionFilter selFilter = new SelectionFilter(filterList);
PromptSelectionResult selectionResult = acDoc.Editor.SelectAll(selFilter);
if (selectionResult.Status == PromptStatus.OK)
{
SelectionSet selectionSet = selectionResult.Value;
// Create a DBObjectCollection to hold the curves
DBObjectCollection curveCollection = new DBObjectCollection();
foreach (ObjectId objectId in selectionSet.GetObjectIds())
{
Entity entity = acTrans.GetObject(objectId, OpenMode.ForRead) as Entity;
if (entity != null)
{
if (entity is Circle || entity is Polyline)
{
curveCollection.Add(entity);
}
}
}
// Create individual regions from curves
DBObjectCollection regionCollection = Region.CreateFromCurves(curveCollection);
// Create a single region from all the regions using Union operation
Region finalRegion = null;
foreach (Region region in regionCollection)
{
if (finalRegion == null)
{
finalRegion = region;
}
else
{
// Union operation to combine regions
finalRegion.BooleanOperation(BooleanOperationType.BoolUnite, region);
}
}
if (finalRegion != null)
{
// Append the final region to the Model space
finalRegion.Layer = "Turvaala"; // Set the layer to "Turvaala"
acBlkTblRec.AppendEntity(finalRegion);
acTrans.AddNewlyCreatedDBObject(finalRegion, true);
}
else
{
acDoc.Editor.WriteMessage("\nFailed to create a region from the selected curves.");
}
// Commit the transaction
acTrans.Commit();
// Return the final region
return finalRegion;
}
return null;
}
}
private void DeleteCreatedEntities()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTableRecord modelSpace = tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForWrite) as BlockTableRecord;
// Filter for polylines and circles on the "Turvaala" layer
TypedValue[] filterList = new TypedValue[]
{
new TypedValue((int)DxfCode.LayerName, "Turvaala"),
new TypedValue((int)DxfCode.Start, "LWPOLYLINE,CIRCLE")
};
SelectionFilter selFilter = new SelectionFilter(filterList);
PromptSelectionResult selectionResult = doc.Editor.SelectAll(selFilter);
if (selectionResult.Status == PromptStatus.OK)
{
SelectionSet selectionSet = selectionResult.Value;
foreach (ObjectId objectId in selectionSet.GetObjectIds())
{
Entity entity = tr.GetObject(objectId, OpenMode.ForWrite) as Entity;
if (entity != null && !entity.IsErased)
{
entity.Erase();
}
}
}
tr.Commit();
}
}
}
Solved! Go to Solution.