/// <summary> /// Gets the polyline points. /// </summary> /// <param name="poid">The poid.</param> /// <returns>Point2dCollection.</returns> private static Point2dCollection GetPolylinePoints(Acaddb.ObjectId poid) { Point2dCollection points; using (Acaddb.Transaction tr = Active.StartTransaction()) { points = new Point2dCollection(); Acaddb.DBObject obj = tr.GetObject(poid, Acaddb.OpenMode.ForRead); Acaddb.Polyline lwp = obj as Acaddb.Polyline; if (lwp != null) { if (lwp.Closed) { int vn = lwp.NumberOfVertices; for (int i = 0; i < vn; i++) { Point2d pt = lwp.GetPoint2dAt(i); points.Add(pt); } } } } return(points); }
/// <summary> /// Scales the object. /// </summary> /// <param name="polyId">The poly identifier.</param> public static void ScaleObject(Acaddb.ObjectId polyId) { try { MessengerManager.MessengerManager.AddLog("Start Scaling"); var offset = -0.01; using (var acTrans = Application.DocumentManager.MdiActiveDocument .TransactionManager.StartTransaction()) { Acaddb.DBObject obj = acTrans.GetObject(polyId, Acaddb.OpenMode.ForWrite); Acaddb.Polyline polyline = obj as Acaddb.Polyline; if (polyline != null) { if (polyline.Closed) { SendPolylineMessage("Length Info Before", polyline); var centroid = GetCentroidOfOuterBoundary(GetPolylinePoints(polyId)); var centroid3d = new Point3d(centroid.X, centroid.Y, 0.0); var closepnt = polyline.GetClosestPointTo(centroid3d, true); var radius = polyline.GetDistAtPoint(closepnt); var scaleFac = 1 + (offset / Math.Abs(radius)); polyline.TransformBy(Matrix3d.Scaling(scaleFac, new Point3d(centroid.X, centroid.Y, 0))); SendPolylineMessage("Length Info After", polyline); MessengerManager.MessengerManager.AddLog (String.Format("Scale Info {0},{1},{2},{3},{4}", centroid, centroid3d, closepnt, radius, scaleFac)); } else { SendPolylineMessage("Polyline is not closed! ", polyline); } } else { SendPolylineMessage("Polyline is NULL! ", polyline); } acTrans.Commit(); } MessengerManager.MessengerManager.AddLog("End Scaling"); } catch (Exception ex) { MessengerManager.MessengerManager.LogException(ex); } }
public static void ScaleWaterObject() { // Get the current document and database try { Document acDoc = Application.DocumentManager.MdiActiveDocument; Acaddb.Database acCurDb = acDoc.Database; var pids = GetAllPolylines(); var pidsToDelete = new Acaddb.ObjectIdCollection(); var offset = -0.001; foreach (Acaddb.ObjectId oid in pids) { offset = -0.001; using (acDoc.LockDocument()) { using (var acTrans = Application.DocumentManager.MdiActiveDocument .TransactionManager.StartTransaction()) { Acaddb.DBObject obj = acTrans.GetObject(oid, Acaddb.OpenMode.ForWrite); Acaddb.Polyline polyline = obj as Acaddb.Polyline; if (polyline != null) { if (polyline.Closed) { if (polyline.Layer != "OWA") { continue; } var centroid = GetCentroidOfOuterBoundary(GetPolylinePoints(oid)); var centroid3d = new Point3d(centroid.X, centroid.Y, 0.0); var closepnt = polyline.GetClosestPointTo(centroid3d, true); var radius = polyline.GetDistAtPoint(closepnt); var scaleFac = 1 + offset / Math.Abs(radius); polyline.TransformBy(Matrix3d.Scaling(scaleFac, new Point3d(centroid.X, centroid.Y, 0))); } } acTrans.Commit(); } } } } catch (Exception ex) { MessengerManager.MessengerManager.LogException(ex); } }
public Polyline GetPolyFromObjId(global::Autodesk.AutoCAD.DatabaseServices.ObjectId oid) { using (global::Autodesk.AutoCAD.DatabaseServices.Database db = CivilApplicationManager.WorkingDatabase) { try { using (Transaction tr = db.TransactionManager.StartTransaction()) { global::Autodesk.AutoCAD.DatabaseServices.DBObject obj; try { obj = tr.GetObject(oid, OpenMode.ForRead); } catch (NullReferenceException e) { PGA.MessengerManager.MessengerManager.LogException(e); return(null); } Polyline lwp = obj as Polyline; if (lwp != null) { // Is Polyline Closed if (lwp.Closed) { return(lwp); } } tr.Commit(); } } catch (global::Autodesk.AutoCAD.Runtime.Exception e) { PGA.MessengerManager.MessengerManager.LogException(e); } } return(null); }
/// <summary> /// Gets the polyline. /// </summary> /// <param name="poid">The poid.</param> /// <returns>Acaddb.Polyline.</returns> private static Acaddb.Polyline GetPolyline(Acaddb.ObjectId poid) { using (Acaddb.Transaction tr = Active.StartTransaction()) { Acaddb.DBObject obj = tr.GetObject(poid, Acaddb.OpenMode.ForRead); Acaddb.Polyline lwp = obj as Acaddb.Polyline; if (lwp != null) { if (lwp.Closed) { return(lwp); } } } return(null); }
/// <summary> /// Offsets the features by -0.001 ft /// </summary> /// <param name="name">The name.</param> /// <exception cref="System.ArgumentNullException">layer name</exception> public static void OffsetFeatures(string name) { try { MessengerManager.MessengerManager.AddLog("Start Offset Features"); if (String.IsNullOrEmpty(name)) { throw new ArgumentNullException("Layer Name"); } var pidsToDelete = new Acaddb.ObjectIdCollection(); var pids = GetAllPolylines(); //var refcentroid = GetCentroidOfOuterBoundary(GetPolylinePoints(OGR.ObjectId)); var offset = -0.0001; //inital start value foreach (Acaddb.ObjectId oid in pids) { offset = -0.001; Acaddb.Polyline polyline = GetPolyline((oid)); if (polyline == null) { throw new ArgumentNullException("Polyline is Null"); } if (polyline.Layer != name.ToUpper()) { continue; } MessengerManager.MessengerManager.AddLog("Found Offset Features on Layer " + name); var newpoly = polyline.GetOffsetCurves(offset).Cast <Acaddb.Polyline>(); newpoly.FirstOrDefault().Layer = polyline.Layer; // Get the current document and database var acDoc = Application.DocumentManager.MdiActiveDocument; var acCurDb = acDoc.Database; using (var acTrans = Application.DocumentManager.MdiActiveDocument .TransactionManager.StartTransaction()) { // Open the Block table for read Acaddb.BlockTable acBlkTbl; acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, Acaddb.OpenMode.ForRead) as Acaddb.BlockTable; // Open the Block table record Model space for write Acaddb.BlockTableRecord acBlkTblRec; acBlkTblRec = acTrans.GetObject(acBlkTbl[Acaddb.BlockTableRecord.ModelSpace], Acaddb.OpenMode.ForWrite) as Acaddb.BlockTableRecord; // Add each offset object acBlkTblRec.AppendEntity(newpoly.FirstOrDefault()); acTrans.AddNewlyCreatedDBObject(newpoly.FirstOrDefault(), true); acTrans.Commit(); } pidsToDelete.Add(oid); } DeletePolys(pidsToDelete); } catch (Exception ex) { MessengerManager.MessengerManager.LogException(ex); } finally { MessengerManager.MessengerManager.AddLog("End Offset Features"); } }
/// <summary> /// Sends the polyline message. /// </summary> /// <param name="info">The information.</param> /// <param name="polyline">The polyline.</param> public static void SendPolylineMessage(string info, Acaddb.Polyline polyline) { MessengerManager.MessengerManager.AddLog (String.Format(info + " L={0},A={1}", polyline.Length, polyline.Area)); }
public static void OffsetGreen() { try { var pids = GetAllPolylines(); //var OGR = GetGreen(pids); //if (OGR == null) // return; //var refcentroid = GetCentroidOfOuterBoundary(GetPolylinePoints(OGR.ObjectId)); //var collars = GetCollar(pids); // AdjustCollars(collars); // var roughouts = GetRoughOutline(pids); // AdjustRoughLayouts(roughouts); var offset = -0.0001; var posOffset = 0.0001; foreach (Acaddb.ObjectId oid in pids) { offset = -0.0001; Acaddb.Polyline polyline = GetPolyline((oid)); if (polyline == null) { return; } if (polyline.Layer == "OGR") { continue; } if (polyline.Layer == "OCO") { offset = posOffset; } else if (polyline.Layer == "OIR") { offset = posOffset; } var newpoly = polyline.GetOffsetCurves(offset).Cast <Acaddb.Polyline>(); newpoly.FirstOrDefault().Layer = polyline.Layer; // Get the current document and database var acDoc = Application.DocumentManager.MdiActiveDocument; var acCurDb = acDoc.Database; using (var acTrans = Application.DocumentManager.MdiActiveDocument .TransactionManager.StartTransaction()) { // Open the Block table for read Acaddb.BlockTable acBlkTbl; acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, Acaddb.OpenMode.ForRead) as Acaddb.BlockTable; // Open the Block table record Model space for write Acaddb.BlockTableRecord acBlkTblRec; acBlkTblRec = acTrans.GetObject(acBlkTbl[Acaddb.BlockTableRecord.ModelSpace], Acaddb.OpenMode.ForWrite) as Acaddb.BlockTableRecord; // Add each offset object acBlkTblRec.AppendEntity(newpoly.FirstOrDefault()); acTrans.AddNewlyCreatedDBObject(newpoly.FirstOrDefault(), true); acTrans.Commit(); } } DeletePolys(pids); } catch (Exception ex) { MessengerManager.MessengerManager.LogException(ex); } }
public static Acaddb.ObjectId ScaleNewPolylineObject(Point2dCollection points) { var offset = -0.001; var newoid = Acaddb.ObjectId.Null; var doc = Active.Document; try { // Get the current document and database Document acDoc = Application.DocumentManager.MdiActiveDocument; Acaddb.Database acCurDb = acDoc.Database; using (acDoc.LockDocument()) { // Start a transaction using (Acaddb.Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) { // Open the Block table for read Acaddb.BlockTable acBlkTbl; acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, Acaddb.OpenMode.ForRead) as Acaddb.BlockTable; // Open the Block table record Model space for write Acaddb.BlockTableRecord acBlkTblRec; acBlkTblRec = acTrans.GetObject(acBlkTbl[Acaddb.BlockTableRecord.ModelSpace], Acaddb.OpenMode.ForWrite) as Acaddb.BlockTableRecord; // Create a lightweight polyline using (Acaddb.Polyline acPoly = new Acaddb.Polyline()) { int i = 0; foreach (Point2d pnt in points) { acPoly.AddVertexAt(i++, pnt, 0, 0, 0); } // Close the polyline acPoly.Closed = true; SendPolylineMessage("Length Info Before", acPoly); var centroid = GetCentroidOfOuterBoundary(points); var centroid3d = new Point3d(centroid.X, centroid.Y, 0.0); var closepnt = acPoly.GetClosestPointTo(centroid3d, true); var radius = acPoly.GetDistAtPoint(closepnt); var scaleFac = 1 + (offset / Math.Abs(radius)); acPoly.TransformBy(Matrix3d.Scaling(scaleFac, new Point3d(centroid.X, centroid.Y, 0))); SendPolylineMessage("Length Info After", acPoly); // Add the new object to the block table record and the transaction newoid = acBlkTblRec.AppendEntity(acPoly); acTrans.AddNewlyCreatedDBObject(acPoly, true); // Save the new objects to the database acTrans.Commit(); } } } } catch (Exception ex) { MessengerManager.MessengerManager.LogException(ex); } return(newoid); }