public static Polyline GetPolyLineObject(ObjectId selectedObjectId, global::Autodesk.AutoCAD.DatabaseServices.Database db) { Transaction tr = db.TransactionManager.StartTransaction(); // Get the current UCS using (tr) { DBObject obj = tr.GetObject(selectedObjectId, OpenMode.ForRead); Polyline lwp = obj as Polyline; if (lwp != null) { if (lwp.Closed) { return(lwp); } } tr.Commit(); } return(null); }
public static ObjectId GetSmallestAreaOfOCO(ObjectIdCollection polylines, global::Autodesk.AutoCAD.DatabaseServices.Database db) { double MaxArea = 1000000; ObjectId pid = ObjectId.Null; foreach (ObjectId master in polylines) { var masterLayer = SelectPolylines.GetPolyLineObject(master, db).Layer; var masterArea = SelectPolylines.GetPolyLineArea(master, db); if ((double)masterArea == 0) { continue; } if (masterLayer.Equals("OCO")) { if (masterArea < MaxArea) { MaxArea = (double)masterArea; pid = master; } } } return(pid); }
/// <summary> /// Adds the predefined hatch. /// </summary> /// <param name="trans">The trans.</param> /// <param name="points">The points.</param> /// <param name="layername">The layername.</param> /// <param name="isClosed">if set to <c>true</c> [is closed].</param> /// <param name="patternName">Name of the pattern.</param> /// <returns></returns> public static ObjectId AddPredefinedHatch(Transaction trans, Point2dCollection points, string layername, bool isClosed, string patternName) { PGA.MessengerManager.MessengerManager.AddLog("Start AddPredefinedHatch"); ObjectId id = ObjectId.Null; global::Autodesk.AutoCAD.DatabaseServices.Database db = Active.Database; var tm = db.TransactionManager; using (Hatch hatch = new Hatch()) { hatch.SetDatabaseDefaults(); hatch.Layer = layername; id = AddToDatabase(db, hatch, trans); hatch.UpgradeOpen(); //hatch.HatchStyle = HatchStyle.Outer; hatch.Associative = true; hatch.PatternScale = 100.0; hatch.SetHatchPattern(HatchPatternType.PreDefined, patternName); double buldge = 0.0; HatchLoop loop = new HatchLoop(HatchLoopTypes.Polyline); foreach (Point2d pt in points) { BulgeVertex bv = new BulgeVertex(pt, buldge); loop.Polyline.Add(bv); } hatch.AppendLoop(loop); hatch.EvaluateHatch(true); } PGA.MessengerManager.MessengerManager.AddLog("End AddPredefinedHatch"); return(id); }
public static void ExportSurfaceObjects() { PasteSurfaces surfaceManager = new PasteSurfaces(); global::Autodesk.AutoCAD.DatabaseServices.Database db = Application.DocumentManager.MdiActiveDocument.Database; Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; try { ObjectIdCollection surfaces = surfaceManager.GetAllSurfaces(); foreach (ObjectId surfaceId in surfaces) { using (Transaction trans = db.TransactionManager.StartTransaction()) { TinSurface surface = trans.GetObject(surfaceId, OpenMode.ForWrite) as TinSurface; surface.GetTriangles(true); trans.Commit(); } } } catch (Exception e) { PGA.MessengerManager.MessengerManager.LogException(e); } }
/// <summary> /// Adds an entity to the database and returns the id /// </summary> /// <param name="db">The db.</param> /// <param name="databaseObject">The database object.</param> /// <param name="trans">The trans.</param> /// <returns> /// ObjectId of the entity after it is added to the database /// </returns> public static ObjectId AddToDatabase(global::Autodesk.AutoCAD.DatabaseServices.Database db, Entity databaseObject, Transaction trans) { ObjectId id = ObjectId.Null; PGA.MessengerManager.MessengerManager.AddLog("Start AddToDatabase"); try { using (BlockTable blockTable = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead)) { using (BlockTableRecord btr = (BlockTableRecord)trans.GetObject(blockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite)) { id = btr.AppendEntity(databaseObject); trans.AddNewlyCreatedDBObject(databaseObject, true); } } PGA.MessengerManager.MessengerManager.AddLog("End AddToDatabase"); return(id); } //catch (Autodesk.AutoCAD.Runtime.Exception arex) //{ // //PGA.MessengerManager.MessengerManager.AddLog("Object already in database", arex); // //System.Diagnostics.Debug.WriteLine(databaseObject.GetType().Name + " already in database"); //} catch (System.Exception ex) { PGA.MessengerManager.MessengerManager.AddLog("Error adding object to the database", ex); throw; } }
public static Polyline GetPolyLineObject(ObjectId selectedObjectId, global::Autodesk.AutoCAD.DatabaseServices.Database db) { //Document doc = Application.DocumentManager.MdiActiveDocument; //Editor ed = doc.Editor; //Database db = doc.Database; Transaction tr = db.TransactionManager.StartTransaction(); // Get the current UCS using (tr) { DBObject obj = tr.GetObject(selectedObjectId, OpenMode.ForRead); // If a "lightweight" (or optimized) polyline Polyline lwp = obj as Polyline; if (lwp != null) { if (lwp.Closed) { return(lwp); } } tr.Commit(); } return(null); }
public static double?GetPolyLineArea(ObjectId selectedObjectId, global::Autodesk.AutoCAD.DatabaseServices.Database db) { //Document doc = Application.DocumentManager.MdiActiveDocument; //Editor ed = doc.Editor; //Database db = doc.Database; Transaction tr = db.TransactionManager.StartTransaction(); // Get the current UCS //CoordinateSystem3d ucs = // ed.CurrentUserCoordinateSystem.CoordinateSystem3d; using (tr) { DBObject obj = tr.GetObject(selectedObjectId, OpenMode.ForRead); // If a "lightweight" (or optimized) polyline Polyline lwp = obj as Polyline; if (lwp != null) { if (lwp.Closed) { return(lwp.Area); } } else { // If an old-style, 2D polyline Polyline2d p2d = obj as Polyline2d; if (p2d != null) { return(p2d.Area); } else { // If an old-style, 3D polyline Polyline3d p3d = obj as Polyline3d; if (p3d != null) { return(p3d.Area); } } } tr.Commit(); } return(null); }
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 static double?GetPolyLineArea(ObjectId selectedObjectId, global::Autodesk.AutoCAD.DatabaseServices.Database db) { Transaction tr = db.TransactionManager.StartTransaction(); using (tr) { DBObject obj = tr.GetObject(selectedObjectId, OpenMode.ForRead); // If a "lightweight" (or optimized) polyline Polyline lwp = obj as Polyline; if (lwp != null) { if (lwp.Closed) { return(lwp.Area); } } else { // If an old-style, 2D polyline Polyline2d p2d = obj as Polyline2d; if (p2d != null) { return(p2d.Area); } else { // If an old-style, 3D polyline Polyline3d p3d = obj as Polyline3d; if (p3d != null) { return(p3d.Area); } } } tr.Commit(); } return(null); }
// /// <summary> // /// Gets the object identifier. // /// </summary> // /// <param name="handle">The handle.</param> // /// <returns>ObjectId.</returns> public static ObjectId GetObjectId(string handle) { ObjectId id = ObjectId.Null; Document doc = Application.DocumentManager.MdiActiveDocument; global::Autodesk.AutoCAD.DatabaseServices.Database db = doc.Database; using (Transaction tr = db.TransactionManager.StartTransaction()) { Handle h = new Handle(Int64.Parse (handle, NumberStyles.AllowHexSpecifier)); db.TryGetObjectId(h, out id); tr.Commit(); } return(id); }
/// <summary> /// Adds the DB text. /// </summary> /// <param name="trans">The trans.</param> /// <param name="insert">The insert.</param> /// <param name="text">The text.</param> /// <param name="layerId">The layer id.</param> /// <param name="textStyleId">The text style id.</param> /// <param name="height">The height.</param> /// <param name="rotation">The rotation.</param> /// <param name="hoizonatlMode">The hoizonatl mode.</param> /// <returns></returns> public static ObjectId AddDBText(Transaction trans, Point3d insert, string text, string layer, string textStyle, double height, double rotation, TextHorizontalMode horizontalMode) { PGA.MessengerManager.MessengerManager.AddLog("Start AddDBText"); try { global::Autodesk.AutoCAD.DatabaseServices.Database db = Active.Database; ObjectId id = ObjectId.Null; DBText itemText = new DBText(); itemText.SetDatabaseDefaults(); itemText.Layer = layer; itemText.Height = height; itemText.Rotation = rotation; itemText.TextString = text; itemText.HorizontalMode = TextHorizontalMode.TextCenter; if (horizontalMode == TextHorizontalMode.TextCenter) { itemText.AlignmentPoint = insert; } else { itemText.Position = insert; } using (TextStyleTable textStyleTable = (TextStyleTable)trans.GetObject(db.TextStyleTableId, OpenMode.ForRead, false)) { if (textStyleTable.Has(textStyle)) { itemText.TextStyleId = textStyleTable[textStyle]; } } id = AddToDatabase(db, itemText, trans); PGA.MessengerManager.MessengerManager.AddLog("End AddDBText"); return(id); } catch (System.Exception ex) { PGA.MessengerManager.MessengerManager.AddLog("Error in AddDBText ", ex); throw; } }
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> /// Adds the M text. /// </summary> /// <param name="trans">The trans.</param> /// <param name="insert">The insert.</param> /// <param name="text">The text.</param> /// <param name="layer">The layer.</param> /// <param name="textStyle">The text style.</param> /// <param name="height">The height.</param> /// <param name="rotation">The rotation.</param> /// <param name="colorIndex">Index of the color.</param> /// <returns></returns> public static ObjectId AddMText(Transaction trans, Point3d insert, string text, string layer, string textStyle, double height, double rotation, int colorIndex) { PGA.MessengerManager.MessengerManager.AddLog("Start AddMText"); try { global::Autodesk.AutoCAD.DatabaseServices.Database db = Active.Database; ObjectId id = ObjectId.Null; MText itemText = new MText(); itemText.SetDatabaseDefaults(); itemText.Location = insert; itemText.Layer = layer; if (height > 0) { itemText.TextHeight = height; } itemText.Rotation = rotation; itemText.Attachment = AttachmentPoint.MiddleCenter; itemText.Contents = text; itemText.ColorIndex = colorIndex; using (TextStyleTable textStyleTable = (TextStyleTable)trans.GetObject(db.TextStyleTableId, OpenMode.ForRead, false)) { if (textStyleTable.Has(textStyle)) { itemText.TextStyleId = textStyleTable[textStyle]; } } id = AddToDatabase(db, itemText, trans); PGA.MessengerManager.MessengerManager.AddLog("End AddMText"); return(id); } catch (System.Exception ex) { PGA.MessengerManager.MessengerManager.AddLog("Error in AddMText ", ex); throw; } }
/// <summary> /// Sets the display order. /// </summary> /// <param name="transaction">The transaction.</param> /// <param name="oids">The object ids.</param> /// <param name="sendToTop">The draw order.</param> public static void SetDisplayOrder(Transaction transaction, ObjectIdCollection oids, bool sendToTop) { PGA.MessengerManager.MessengerManager.AddLog("Start SetDisplayOrder"); if (oids == null) { throw new NullReferenceException("oids is null"); } // verify the arguments // if (oids.Count == 0) { return; } // get the database from the the first objectId // global::Autodesk.AutoCAD.DatabaseServices.Database db = oids[0].Database; // create a transaction if needed // Transaction trans = null; if (transaction == null) { trans = db.TransactionManager.StartTransaction(); } else { trans = transaction; } // now get the draworder table and set accordingly // using (BlockTable blockTable = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead)) { using (BlockTableRecord btr = (BlockTableRecord)trans.GetObject(blockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite)) { using (DrawOrderTable drawOrderTable = trans.GetObject(btr.DrawOrderTableId, OpenMode.ForWrite) as DrawOrderTable) { if (drawOrderTable != null) { if (sendToTop) { drawOrderTable.MoveToTop(oids); } else { drawOrderTable.MoveToBottom(oids); } } } } } // cleanup transaction if needed // if (transaction == null) { trans.Commit(); trans.Dispose(); } PGA.MessengerManager.MessengerManager.AddLog("End SetDisplayOrder"); }
/// <summary> /// Removes the duplicate surfaces by using additional filtering. /// looking for isolated polylines with no internal objects /// on same layer. /// </summary> public static void RemoveDuplicateSurfaces_V2() { try { COMS.AddLog("Entering Remove Duplicate Surfaces V2!"); DatabaseCommands commands = new DatabaseCommands(); // var filename = OpenDwg.file; ObjectIdCollection polylines = null; var excludedpolys = new List <Handle>(); //var features = commands.GetAllFeatures(); #region For Testing Open File //Document doc = OpenDwg.OpenDwgForWork(filename); //Application.DocumentManager.MdiActiveDocument = doc; //var db = doc.Database; //var ed = doc.Editor; #endregion Document doc = Application.DocumentManager.MdiActiveDocument; global::Autodesk.AutoCAD.DatabaseServices.Database db = doc.Database; using (doc.LockDocument()) { using (var trans = db.TransactionManager.StartTransaction()) { polylines = ACADUTIL.GetAllObjectIdsInModel(db, trans, false); ObjectId MaxORO = GetLargestAreaOfORO(polylines, db); ObjectId MinOCO = GetSmallestAreaOfOCO(polylines, db); ObjectId MinOIR = GetSmallestAreaOfOIR(polylines, db); foreach (ObjectId master in polylines) { var masterpolyline = SelectPolylines.GetPolyLineObject(master, db); var masterArea = SelectPolylines.GetPolyLineArea(master, db); foreach (ObjectId comparer in polylines) { //cannot be identical oids if (master.Equals(comparer)) { continue; } //get areas of polys var comparerpolyline = SelectPolylines.GetPolyLineObject(comparer, db); var comparerArea = SelectPolylines.GetPolyLineArea(comparer, db); System.Diagnostics.Debug.WriteLine(String.Format("M={0},C={1}", masterArea, comparerArea)); if (comparerArea == null || masterArea == null) { continue; } //Get Internal Polylines to Interate var IsInternal = PointInPolyline(ACADUTIL.GetPointsFromPolyline(masterpolyline), ACADUTIL.GetPointsFromPolyline(comparerpolyline)); if ((masterArea > comparerArea) && IsInternal) { System.Diagnostics.Debug.WriteLine(String.Format ("We are Internal! M={0},C={1}", masterArea, comparerArea)); // Does master have any internals on same layer // We are looking for isolated polylines with no internal objects // on same layer. var masterLayer = SelectPolylines.GetPolyLineObject(master, db).Layer; var comparerLayer = SelectPolylines.GetPolyLineObject(comparer, db).Layer; //filter out largest ORO polyline if (masterLayer == "ORO" && master.Equals(MaxORO)) { continue; } if (masterLayer == "OCO" && master.Equals(MinOCO)) { excludedpolys.Add(master.Handle); continue; } #region Removed-5.14.16 //if (masterLayer == "OIR" && master.Equals(MinOIR)) //{ // excludedpolys.Add(master.Handle); // continue; //} #endregion //continue searching if has internal with same layer #region Exclude matching layers if (masterLayer.Equals(comparerLayer)) { continue; } #endregion #region Add Additional Rank Filtering //If Green and Fairway, then exclude it! int Green = 1; int Fairway = 3; int IntRough = 21; //Get Rank and exclude secondary poly int masterRank = commands.GetFeatureRankByCode(masterLayer); int comparRank = commands.GetFeatureRankByCode(comparerLayer); COMS.AddLog(String.Format( "Debugging = ML={0},CL={1}, MR={2} CR={3} ", masterLayer, comparerLayer, masterRank, comparRank)); if (masterRank == IntRough) { continue; } if ((masterRank > comparRank) && (masterRank > 10) && (comparRank == Green || comparRank == Fairway)) { //exclude master layer excludedpolys.Add(master.Handle); COMS.AddLog(String.Format( "Exclude Comparer by Layer Filtering = {0},{1}, MA={2} ML={3} ", comparerLayer, comparerArea, masterArea, masterLayer)); } #endregion } } } trans.Commit(); } foreach (var id in excludedpolys) { commands.InsertToExcludedFeatures(id.ToString()); } } COMS.AddLog("ExitingRemove Duplicate Surfaces V2!"); } catch (Exception ex) { COMS.LogException(ex); } }
/// <summary> /// Removes the duplicate surfaces by using Layer Rank. /// </summary> public static void RemoveDuplicateSurfaces_V1() { try { COMS.AddLog("Entering Remove Duplicate Surfaces V1!"); DatabaseCommands commands = new DatabaseCommands(); var excludedpolys = new List <Handle>(); ObjectIdCollection polylines = null; //var features = commands.GetAllFeatures(); #region Testing for Opening DWGs //var filename = OpenDwg.file; //Document doc = Application.DocumentManager.Open(filename, true); //Application.DocumentManager.MdiActiveDocument = doc; //var db = doc.Database; //var ed = doc.Editor; #endregion Document doc = Application.DocumentManager.MdiActiveDocument; global::Autodesk.AutoCAD.DatabaseServices.Database db = doc.Database; using (doc.LockDocument()) { using (var trans = db.TransactionManager.StartTransaction()) { PGA.SimplifyPolylines.Commands.SimplifyPolylinesTest(); //polylines = SelectPolylines.GetAllPolylines(); polylines = ACADUTIL.GetAllObjectIdsInModel(db, trans, false); foreach (ObjectId master in polylines) { var masterArea = SelectPolylines.GetPolyLineArea(master, db); foreach (ObjectId comparer in polylines) { //cannot be identical oids if (master.Equals(comparer)) { continue; } //get areas of polys var comparerArea = SelectPolylines.GetPolyLineArea(comparer, db); System.Diagnostics.Debug.WriteLine(String.Format ("M={0},C={1}", masterArea, comparerArea)); if (comparerArea == null || masterArea == null) { continue; } //Precision is set to < 0.5 Sq-ft Missed Features //Precision is set to = 1.0 Sq-ft Works(Not Perfect) if (IsEqual(masterArea, comparerArea, 1.0)) { System.Diagnostics.Debug.WriteLine(String.Format("Entering Equals--> M={0},C={1}", masterArea, comparerArea)); var masterLayer = SelectPolylines.GetPolyLineObject(master, db).Layer; var comparerLayer = SelectPolylines.GetPolyLineObject(comparer, db).Layer; //Get Rank and exclude secondary poly if (commands.GetFeatureRankByCode(masterLayer) > commands.GetFeatureRankByCode(comparerLayer)) { //exclude comparer layer excludedpolys.Add(master.Handle); COMS.AddLog(String.Format( "Exclude Master by Layer Rank = CL={0},CA={1}, MA={2} ML={3} ", comparerLayer, comparerArea, masterArea, masterLayer)); } } } } trans.Commit(); } commands.ClearExcludedFeatures(); foreach (var id in excludedpolys) { commands.InsertToExcludedFeatures(id.ToString()); } } COMS.AddLog("Exiting Remove Duplicate Surfaces V1!"); } catch (Exception ex) { COMS.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); }