hi all
I'm trying to make a region out of Polyline3d
Build
But I get a message like in the picture
It is a possible helper to solve the error
Thank you all
The original code from the page
https://www.theswamp.org/index.php?topic=43572.0
/// BrepSamples.cs (excerpt) Copyright (c) 2008 Tony Tanzillo
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.BoundaryRepresentation; // acdbmgdbrep.dll
using Autodesk.AutoCAD.Geometry;
using AcBr = Autodesk.AutoCAD.BoundaryRepresentation;
using ErrorStatus = Autodesk.AutoCAD.Runtime.ErrorStatus;
namespace BRepSamples
{
public static class Containment
{
/// <summary>
///
/// This variation performs point contianment testing
/// against any closed curve from which a simple region
/// (e.g., no inner-loops) can be genreated, using the
/// Region.CreateFromCurves() API.
///
/// </summary>
[CommandMethod("testCURVECONTAINMENT")]
public static void CurveContainmentCommand()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
var doc = Application.DocumentManager.MdiActiveDocument;
var db = doc.Database;
ObjectId id = GetCurve(ed, "\nSelect a closed Curve: ");
if (!id.IsNull)
{
using (Transaction tr = ed.Document.TransactionManager.StartTransaction())
{
try
{
Curve curve = (Curve)id.GetObject(OpenMode.ForRead);
if (!curve.Closed)
{
ed.WriteMessage("\nInvalid selection, requires a CLOSED curve");
return;
}
if (id.ObjectClass == RXObject.GetClass(typeof(Polyline3d)))
{
var poly3d = (Polyline3d)tr.GetObject(id, OpenMode.ForWrite);
var space = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
try
{
Polyline poly2dx = new Polyline();
int j = 0;
foreach (ObjectId vid in poly3d)
{
PolylineVertex3d v3d = (PolylineVertex3d)tr.GetObject(vid, OpenMode.ForRead);
var planez = new Plane(Point3d.Origin, Vector3d.ZAxis);
//poly2dx.AddVertexAt(j, new Point3d(v3d.Position.X, v3d.Position.Y, v3d.Position.Z).Convert2d(planez), 0, 0, 0);
poly2dx.AddVertexAt(j, new Point2d(v3d.Position.X, v3d.Position.Y), 0, 0, 0);
j++;
}
poly2dx.Closed = true;
space.AppendEntity(poly2dx);
tr.AddNewlyCreatedDBObject(poly2dx, true);
Curve curve0 = (Curve)poly2dx.Id.GetObject(OpenMode.ForRead);
using (Region region = RegionFromClosedCurve(curve))
{
PromptPointOptions ppo = new PromptPointOptions("\nSelect a point: ");
ppo.AllowNone = true;
while (true) // loop while user continues to pick points
{
/// Get a point from user:
PromptPointResult ppr = ed.GetPoint(ppo);
if (ppr.Status != PromptStatus.OK) // no point was selected, exit
break;
// use the GetPointContainment helper method below to
// get the PointContainment of the selected point:
PointContainment containment = GetPointContainment(region, ppr.Value);
// Display the result:
ed.WriteMessage("\nPointContainment = {0}", containment.ToString());
}
}
}
catch (System.Exception ex)
{
Application.ShowAlertDialog(ex.Message);
}
}
else
using (Region region = RegionFromClosedCurve(curve))
{
PromptPointOptions ppo = new PromptPointOptions("\nSelect a point: ");
ppo.AllowNone = true;
while (true) // loop while user continues to pick points
{
/// Get a point from user:
PromptPointResult ppr = ed.GetPoint(ppo);
if (ppr.Status != PromptStatus.OK) // no point was selected, exit
break;
// use the GetPointContainment helper method below to
// get the PointContainment of the selected point:
PointContainment containment = GetPointContainment(region, ppr.Value);
// Display the result:
ed.WriteMessage("\nPointContainment = {0}", containment.ToString());
}
}
}
finally
{
tr.Commit();
}
}
}
}
// this helper method takes a Region and a Point3d that must be
// in the plane of the region, and returns the PointContainment
// of the given point, adjusted to correctly indicate its actual
// relationship to the region (inside/on boundary edge/outside).
public static PointContainment GetPointContainment(Region region, Point3d point)
{
PointContainment result = PointContainment.Outside;
/// Get a Brep object representing the region:
using (Brep brep = new Brep(region))
{
if (brep != null)
{
// Get the PointContainment and the BrepEntity at the given point:
using (BrepEntity ent = brep.GetPointContainment(point, out result))
{
if (ent is AcBr.Face)
result = PointContainment.Inside;
}
}
}
return result;
}
public static Region RegionFromClosedCurve(Curve curve)
{
if (!curve.Closed)
throw new ArgumentException("Curve must be closed.");
DBObjectCollection curves = new DBObjectCollection();
curves.Add(curve);
using (DBObjectCollection regions = Region.CreateFromCurves(curves))
{
if (regions == null || regions.Count == 0)
throw new InvalidOperationException("Failed to create regions");
if (regions.Count > 1)
throw new InvalidOperationException("Multiple regions created");
return regions.Cast<Region>().First();
}
}
public static PointContainment GetPointContainment(Curve curve, Point3d point)
{
if (!curve.Closed)
throw new ArgumentException("Curve must be closed.");
Region region = RegionFromClosedCurve(curve);
if (region == null)
throw new InvalidOperationException("Failed to create region");
using (region)
{
return GetPointContainment(region, point);
}
}
public static ObjectId GetCurve(Editor editor, string msg)
{
PromptEntityOptions peo = new PromptEntityOptions(msg);
peo.SetRejectMessage("Invalid selection: requires a closed Curve,");
peo.AddAllowedClass(typeof(Curve), false);
PromptEntityResult res = editor.GetEntity(peo);
return res.Status == PromptStatus.OK ? res.ObjectId : ObjectId.Null;
}
}
}
Solved! Go to Solution.
Solved by hosneyalaa. Go to Solution.
Solved by R.Gerritsen4967. Go to Solution.
Your problem is with the polyline3D in the drawing.
The polyline3d is not planar, so it is not possible to make a region of it.
You can check if the selected polyline3d is planar with the following command:
polyline3d.IsPlanar
Which is a boolean value (True/False).
Thank you
I forgot to change the item
using (Region region = RegionFromClosedCurve(curve0))
Thanks
Can't find what you're looking for? Ask the community or share your knowledge.