private void createX(_Db.BlockTableRecord btr, double radius, _Ge.Point3d ip) { _Ge.Point2d p1 = new _Ge.Point2d(ip.X - radius, ip.Y); _Ge.Point2d p2 = new _Ge.Point2d(ip.X + radius, ip.Y); _Ge.Point2d p3 = new _Ge.Point2d(ip.X, ip.Y - radius); _Ge.Point2d p4 = new _Ge.Point2d(ip.X, ip.Y + radius); using (_Db.Polyline poly = new _Db.Polyline()) { poly.AddVertexAt(0, p1, 0, 0, 0); poly.AddVertexAt(1, p2, 0, 0, 0); poly.ColorIndex = 2; btr.AppendEntity(poly); _c.trans.AddNewlyCreatedDBObject(poly, true); } using (_Db.Polyline poly = new _Db.Polyline()) { poly.AddVertexAt(0, p3, 0, 0, 0); poly.AddVertexAt(1, p4, 0, 0, 0); poly.ColorIndex = 2; btr.AppendEntity(poly); _c.trans.AddNewlyCreatedDBObject(poly, true); } }
public static void BuildingOrthogonalPolylines() { try { AcDb.Database db = CurrentCAD.Database; jigger = new OrthogonalPolylinesJig(); AcEd.PromptResult jigRes; do { jigRes = CurrentCAD.Editor.Drag(jigger); if (jigRes.Status == AcEd.PromptStatus.OK) { jigger.allVertexes.Add(jigger.lastVertex); } } while (jigRes.Status == AcEd.PromptStatus.OK); using (AcDb.Transaction tr = db.TransactionManager.StartTransaction()) { AcDb.BlockTableRecord btr = (AcDb.BlockTableRecord)tr.GetObject(db.CurrentSpaceId, AcDb.OpenMode.ForWrite); Teigha.DatabaseServices.Polyline ent = new Teigha.DatabaseServices.Polyline(); ent.SetDatabaseDefaults(); for (int i = 0; i < jigger.allVertexes.Count; i++) { AcGe.Point3d pt3d = jigger.allVertexes[i]; AcGe.Point2d pt2d = new AcGe.Point2d(pt3d.X, pt3d.Y); ent.AddVertexAt(i, pt2d, 0, db.Plinewid, db.Plinewid); } ent.TransformBy(jigger.UCS); btr.AppendEntity(ent); tr.AddNewlyCreatedDBObject(ent, true); tr.Commit(); } } catch (System.Exception ex) { CurrentCAD.Editor.WriteMessage(ex.ToString()); } }
private List <_Ge.Point3d> getPolylinePoints(_Db.Polyline poly) { List <_Ge.Point3d> points = new List <_Ge.Point3d>(); int verts = poly.NumberOfVertices; for (int i = 1; i < verts; i++) { _Ge.Point3d start = poly.GetPoint3dAt(i - 1); _Ge.Point3d end = poly.GetPoint3dAt(i); double midX = start.X + ((end.X - start.X) / 2); double midY = start.Y + ((end.Y - start.Y) / 2); _Ge.Point3d mid = new _Ge.Point3d(midX, midY, 0); points.Add(start); points.Add(end); points.Add(mid); } if (poly.Closed) { _Ge.Point3d start = poly.GetPoint3dAt(verts - 1); _Ge.Point3d end = poly.GetPoint3dAt(0); double midX = start.X + ((end.X - start.X) / 2); double midY = start.Y + ((end.Y - start.Y) / 2); _Ge.Point3d mid = new _Ge.Point3d(midX, midY, 0); points.Add(mid); } return(points); }
// Transaction extensions ///<summary> /// Create a bounding rectangle around a piece of text (for debugging). ///</summary> ///<param name="txt">The text object around which to create a box.</param> public static void CreateBoundingBox(this _AcDb.Transaction tr, _AcDb.DBText txt) { var ms = tr.GetObject( _AcDb.SymbolUtilityServices.GetBlockModelSpaceId(txt.Database), _AcDb.OpenMode.ForWrite ) as _AcDb.BlockTableRecord; if (ms != null) { var corners = txt.ExtractBounds(); if (corners.Count >= 4) { var doc = _AcAp.Application.DocumentManager.MdiActiveDocument; if (doc == null) { return; } var ed = doc.Editor; var ucs = ed.CurrentUserCoordinateSystem; var cs = ucs.CoordinateSystem3d; var pl = new _AcDb.Polyline(4); for (int i = 0; i < 4; i++) { pl.AddVertexAt(i, corners[i].ProjectToUcs(cs), 0, 0, 0); } pl.Closed = true; pl.TransformBy(ucs); ms.AppendEntity(pl); tr.AddNewlyCreatedDBObject(pl, true); } } }
public static Tuple <List <string>, List <Geometry> > ReadPropertyFromFile(string inputfilename, string layerName) { List <string> layerList = new List <string>(); //图层列表 Database pDb; Transaction trans; List <string> result = new List <string>(); List <Geometry> resultGeo = new List <Geometry>(); using (Services ser = new Services()) { using (pDb = new Database(false, false))//不加参数会出错 { pDb.ReadDwgFile(inputfilename, FileOpenMode.OpenForReadAndReadShare, false, ""); using (trans = pDb.TransactionManager.StartTransaction()) { BlockTableRecord btab = (BlockTableRecord)pDb.CurrentSpaceId.GetObject(OpenMode.ForRead); List <string> bb = new List <string>(); foreach (ObjectId btr in btab) //所有对象遍历 { DBObject obj; try { obj = trans.GetObject(btr, OpenMode.ForWrite); } catch { return(null); }; string a = obj.GetType().Name; if (!bb.Contains(a)) { bb.Add(a); } #region switch switch (obj.GetType().Name) { case "DBPoint": DBPoint dbpoint = (DBPoint)obj; OSGeo.OGR.Geometry point = new OSGeo.OGR.Geometry(wkbGeometryType.wkbPoint); point.AddPoint(dbpoint.Position.X, dbpoint.Position.Y, 0); resultGeo.Add(point); if (dbpoint.Layer != layerName) { break; } result.Add(string.Format("Point---X:{0},T:{1}", dbpoint.Position.X, dbpoint.Position.Y)); break; case "Line": Line line = (Line)obj; if (line.Layer != layerName) { break; } //result.Add(line); break; case "Ellipse": Ellipse ellipse = (Ellipse)obj; if (ellipse.Layer != layerName) { break; } //result.Add(ellipse); break; #region case "Arc": Arc arc = (Arc)obj; if (arc.Layer != layerName) { break; } // result.Add(arc); break; #endregion case "Circle": Circle circle = (Circle)obj; if (circle.Layer != layerName) { break; } result.Add(string.Format("Circle---centerX:{0},centerY:{1},radius:{2}", circle.Center.X, circle.Center.Y, circle.Radius)); //result.Add(circle); break; case "Polyline": Teigha.DatabaseServices.Polyline polyline = (Teigha.DatabaseServices.Polyline)obj; if (polyline.Layer != layerName) { break; } //result.Add(polyline); break; case "DBText": DBText dBText = (DBText)obj; if (dBText.Layer != layerName) { break; } result.Add(string.Format("DBText---TextString:{0}", dBText.TextString)); // result.Add(dBText); break; case "Region": Region region = (Region)obj; if (region.Layer != layerName) { break; } //result.Add(region); break; default: break; } #endregion } } } } return(new Tuple <List <string>, List <Geometry> >(result, resultGeo)); }
private static bool PolyInPoly(_AcDb.TransactionManager tm, _AcDb.ObjectId oid, _AcDb.ObjectId elFG) { using (_AcDb.DBObject pEntity = tm.GetObject(oid, _AcDb.OpenMode.ForRead, false)) { using (_AcDb.DBObject pElFG = tm.GetObject(elFG, _AcDb.OpenMode.ForRead, false)) { if (pEntity is _AcDb.Polyline2d) { _AcDb.Polyline2d oldPolyline = (_AcDb.Polyline2d)pEntity; foreach (_AcDb.ObjectId Vertex2d in oldPolyline) { using (_AcDb.DBObject dbobj = tm.GetObject(Vertex2d, _AcDb.OpenMode.ForRead, false)) { _AcDb.Vertex2d vertex = dbobj as _AcDb.Vertex2d; if (vertex == null) { string msg = string.Format(CultureInfo.CurrentCulture, "Polylinie {0} gibt falsches Objekt {1} als Vertex zurück.", oldPolyline.Handle.ToString(), dbobj.GetType().ToString()); throw new InvalidOperationException(string.Format(msg)); } _AcGe.Point3d vertexPoint = oldPolyline.VertexPosition(vertex); if (!AreaEngine.InPoly(vertexPoint, (_AcDb.Entity)pElFG)) { return(false); } } } return(true); } else if (pEntity is _AcDb.Polyline3d) { _AcDb.Polyline3d poly3d = (_AcDb.Polyline3d)pEntity; foreach (_AcDb.ObjectId Vertex3d in poly3d) { using (_AcDb.DBObject dbobj = tm.GetObject(Vertex3d, _AcDb.OpenMode.ForRead, false)) { _AcDb.PolylineVertex3d vertex = dbobj as _AcDb.PolylineVertex3d; if (vertex == null) { string msg = string.Format(CultureInfo.CurrentCulture, "3D-Polylinie {0} gibt falsches Objekt {1} als Vertex zurück.", poly3d.Handle.ToString(), dbobj.GetType().ToString()); throw new InvalidOperationException(string.Format(msg)); } _AcGe.Point3d vertexPoint = vertex.Position; if (!AreaEngine.InPoly(vertexPoint, (_AcDb.Entity)pElFG)) { return(false); } } } return(true); } else if (pEntity is _AcDb.Polyline) { _AcDb.Polyline poly = pEntity as _AcDb.Polyline; for (int i = 0; i < poly.NumberOfVertices; i++) { _AcGe.Point3d vertexPoint = poly.GetPoint3dAt(i); if (!AreaEngine.InPoly(vertexPoint, (_AcDb.Entity)pElFG)) { return(false); } } return(true); } } } return(false); }
public static void BuildingRectangle() { try { AcDb.Database db = CurrentCAD.Database; AcEd.Editor ed = CurrentCAD.Editor; AcEd.PromptKeywordOptions pko; AcEd.PromptPointOptions ppt; AcEd.PromptPointResult ppr; AcGe.Point3d basePoint; AcGe.Point3d diractionPoint; pko = new AcEd.PromptKeywordOptions("\nПобудова прямокутника"); pko.Keywords.Add("по Діагоналі"); pko.Keywords.Add("по Напрямку та діагоналі"); pko.Keywords.Add("по Ширині та висота"); pko.Keywords.Default = "по Ширині та висота"; pko.AllowNone = false; AcEd.PromptResult pkr = ed.GetKeywords(pko); if (pkr.Status != AcEd.PromptStatus.OK) { return; } MethodConstructingRectangle methodConstructing; if (pkr.StringResult == "Діагоналі") { methodConstructing = MethodConstructingRectangle.Diagonal; } else if (pkr.StringResult == "Напрямку") { methodConstructing = MethodConstructingRectangle.DirectionAndDiagonal; } else { methodConstructing = MethodConstructingRectangle.HeightAndWidth; } ppt = new AcEd.PromptPointOptions("\nВкажіть першу точку прямокутника:"); ppr = ed.GetPoint(ppt); if (ppr.Status != AcEd.PromptStatus.OK) { return; } basePoint = ppr.Value; if (methodConstructing == MethodConstructingRectangle.Diagonal) { diractionPoint = basePoint.Add(AcGe.Vector3d.XAxis); } else { ppt = new AcEd.PromptPointOptions("\n"); if (methodConstructing == MethodConstructingRectangle.DirectionAndDiagonal) { ppt.Message = "\nВкажіть точку напрямку прямокутника:"; } else if (methodConstructing == MethodConstructingRectangle.DirectionAndDiagonal) { ppt.Message = "\nВкажіть ширину прямокутника:"; } ppt.UseBasePoint = true; ppt.BasePoint = basePoint; ppr = ed.GetPoint(ppt); if (ppr.Status != AcEd.PromptStatus.OK) { return; } diractionPoint = ppr.Value; } jigger = new RectangleJig(methodConstructing, basePoint, diractionPoint); ed.Drag(jigger); using (AcDb.Transaction tr = db.TransactionManager.StartTransaction()) { AcDb.BlockTableRecord btr = (AcDb.BlockTableRecord)tr.GetObject(db.CurrentSpaceId, AcDb.OpenMode.ForWrite); Teigha.DatabaseServices.Polyline ent = new Teigha.DatabaseServices.Polyline(); ent.SetDatabaseDefaults(); for (int i = 0; i < jigger.Corners.Count; i++) { AcGe.Point3d pt3d = jigger.Corners[i]; AcGe.Point2d pt2d = new AcGe.Point2d(pt3d.X, pt3d.Y); ent.AddVertexAt(i, pt2d, 0, db.Plinewid, db.Plinewid); } ent.Closed = true; ent.TransformBy(jigger.UCS); btr.AppendEntity(ent); tr.AddNewlyCreatedDBObject(ent, true); tr.Commit(); } } catch (System.Exception ex) { CurrentCAD.Editor.WriteMessage(ex.ToString()); } }
private static bool PolyInPoly(_AcDb.Transaction tm, _AcDb.Entity inner, _AcDb.Entity outer) { if (inner == outer) { return(false); } if (!OverlapExtents(inner, outer)) { return(false); } NrOfOverlaps++; if (inner is _AcDb.Polyline) { _AcDb.Polyline poly = inner as _AcDb.Polyline; for (int i = 0; i < poly.NumberOfVertices; i++) { _AcGe.Point3d vertexPoint = poly.GetPoint3dAt(i); if (!AreaEngine.InPoly(vertexPoint, (_AcDb.Entity)outer)) { return(false); } } return(true); } else if (inner is _AcDb.Polyline2d) { _AcDb.Polyline2d oldPolyline = (_AcDb.Polyline2d)inner; foreach (_AcDb.ObjectId Vertex2d in oldPolyline) { using (_AcDb.DBObject dbobj = tm.GetObject(Vertex2d, _AcDb.OpenMode.ForRead, false)) { _AcDb.Vertex2d vertex = dbobj as _AcDb.Vertex2d; if (vertex == null) { string msg = string.Format(CultureInfo.CurrentCulture, "Polylinie {0} gibt falsches Objekt {1} als Vertex zurück.", oldPolyline.Handle.ToString(), dbobj.GetType().ToString()); throw new InvalidOperationException(string.Format(msg)); } _AcGe.Point3d vertexPoint = oldPolyline.VertexPosition(vertex); if (!AreaEngine.InPoly(vertexPoint, (_AcDb.Entity)outer)) { return(false); } } } return(true); } else if (inner is _AcDb.Polyline3d) { _AcDb.Polyline3d poly3d = (_AcDb.Polyline3d)inner; foreach (_AcDb.ObjectId Vertex3d in poly3d) { using (_AcDb.DBObject dbobj = tm.GetObject(Vertex3d, _AcDb.OpenMode.ForRead, false)) { _AcDb.PolylineVertex3d vertex = dbobj as _AcDb.PolylineVertex3d; if (vertex == null) { string msg = string.Format(CultureInfo.CurrentCulture, "3D-Polylinie {0} gibt falsches Objekt {1} als Vertex zurück.", poly3d.Handle.ToString(), dbobj.GetType().ToString()); throw new InvalidOperationException(string.Format(msg)); } _AcGe.Point3d vertexPoint = vertex.Position; if (!AreaEngine.InPoly(vertexPoint, (_AcDb.Entity)outer)) { return(false); } } } return(true); } throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Unknown entitytype '{0}'!", inner.GetType().Name)); }
private List <G.Line> getGeometry() { List <G.Line> polys = new List <G.Line>(); _Ed.PromptSelectionOptions opts = new _Ed.PromptSelectionOptions(); opts.MessageForAdding = "\nSelect Geometry: "; _Ed.PromptSelectionResult userSelection = _c.doc.Editor.GetSelection(opts); if (userSelection.Status != _Ed.PromptStatus.OK) { throw new DMTException("[ERROR] Geometry - cancelled"); } _Ed.SelectionSet selectionSet = userSelection.Value; foreach (_Ed.SelectedObject currentObject in selectionSet) { if (currentObject == null) { continue; } _Db.Entity currentEntity = _c.trans.GetObject(currentObject.ObjectId, _Db.OpenMode.ForRead) as _Db.Entity; if (currentEntity == null) { continue; } if (currentEntity is _Db.Polyline) { _Db.Polyline poly = _c.trans.GetObject(currentEntity.ObjectId, _Db.OpenMode.ForRead) as _Db.Polyline; int points = poly.NumberOfVertices; for (int i = 1; i < points; i++) { _Ge.Point2d p1 = poly.GetPoint2dAt(i - 1); _Ge.Point2d p2 = poly.GetPoint2dAt(i); G.Point new_p1 = new G.Point(p1.X, p1.Y); G.Point new_p2 = new G.Point(p2.X, p2.Y); if (new_p1 == new_p2) { continue; } G.Line line = new G.Line(new_p1, new_p2); if (!polys.Contains(line)) { polys.Add(line); } } if (poly.Closed) { _Ge.Point2d p1 = poly.GetPoint2dAt(points - 1); _Ge.Point2d p2 = poly.GetPoint2dAt(0); G.Point new_p1 = new G.Point(p1.X, p1.Y); G.Point new_p2 = new G.Point(p2.X, p2.Y); if (new_p1 == new_p2) { continue; } G.Line line = new G.Line(new_p1, new_p2); if (!polys.Contains(line)) { polys.Add(line); } } } } if (polys.Count < 3) { throw new DMTException("[ERROR] Geometry - less then 3"); } return(polys); }