public static void ExtractBorder(/*CivilSurface*/ ITerrainSurface surface) { using (Transaction trans = Tools.StartTransaction()) { var ids = surface.GetBounds(); foreach (ObjectId id in ids) { AcadEntity ent = id.GetObject(OpenMode.ForRead) as AcadEntity; ent = ent.Clone() as AcadEntity; Tools.AppendEntityEx(trans, ent); } } }
clone(ObjectIdCollection ids, Point3d pnt3dBase) { Database db = BaseObjs._db; ObjectIdCollection idsNew = new ObjectIdCollection(); try { using (Transaction tr = BaseObjs.startTransactionDb()) { BlockTableRecord ms = Blocks.getBlockTableRecordMS(); foreach (ObjectId id in ids) { Entity ent = (Entity)tr.GetObject(id, OpenMode.ForRead); Entity entCopy = (Entity)ent.Clone(); ms.AppendEntity(entCopy); tr.AddNewlyCreatedDBObject(entCopy, true); idsNew.Add(entCopy.ObjectId); } db.Dispose(); tr.Commit(); } } catch (System.Exception ex) { BaseObjs.writeDebug(ex.Message + " Blocks.cs: line: 671"); } JigDraw jDraw = new JigDraw(pnt3dBase); jDraw.AddEntity(idsNew); PromptResult jRes = BaseObjs._editor.Drag(jDraw); if (jRes.Status == PromptStatus.OK) { jDraw.TransformEntities(); } else { return; } }
copyXRefEnts(ObjectId idBR, List <ObjectId> idEnts) { ObjectIdCollection ids = new ObjectIdCollection(); try { using (Transaction tr = BaseObjs.startTransactionDb()) { BlockReference br = (BlockReference)tr.GetObject(idBR, OpenMode.ForRead); foreach (ObjectId id in idEnts) { Entity ent = id.getEnt(); object o = ent.Clone(); Entity clone = (Entity)o; if (clone != null) { clone.SetPropertiesFrom(br); BlockTableRecord space = (BlockTableRecord)BaseObjs._db.CurrentSpaceId.GetObject(OpenMode.ForWrite); if (space == null) { clone.Dispose(); return(null); } space.AppendEntity(clone); tr.AddNewlyCreatedDBObject(clone, true); ids.Add(clone.ObjectId); } } tr.Commit(); } } catch (System.Exception ex) { BaseObjs.writeDebug(ex.Message + " xRef.cs: line: 198"); } return(ids); }
OnPromptForEntityEnding(object sender, PromptForEntityEndingEventArgs e) { if (e.Result.Status == PromptStatus.OK) { Editor ed = sender as Editor; ObjectId objId = e.Result.ObjectId; Database db = objId.Database; try { using (Transaction tr = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database.TransactionManager.StartTransaction()) { // First get the currently selected object and check whether it's a block reference BlockReference br = tr.GetObject(objId, OpenMode.ForRead) as BlockReference; if (br != null) { // If so, we check whether the block table record to which it refers is actually from an XRef ObjectId btrId = br.BlockTableRecord; BlockTableRecord btr = tr.GetObject(btrId, OpenMode.ForRead) as BlockTableRecord; if (btr != null) { if (btr.IsFromExternalReference) { // If so, then we programmatically select the object underneath the pick-point already used PromptNestedEntityOptions pneo = new PromptNestedEntityOptions(""); pneo.NonInteractivePickPoint = e.Result.PickedPoint; pneo.UseNonInteractivePickPoint = true; PromptNestedEntityResult pner = ed.GetNestedEntity(pneo); if (pner.Status == PromptStatus.OK) { try { ObjectId selId = pner.ObjectId; // Let's look at this programmatically-selected object, to see what it is DBObject obj = selId.GetObject(OpenMode.ForRead); // If it's a polyline vertex, we need to go one level up to the polyline itself if (obj is PolylineVertex3d || obj is Vertex2d) { selId = obj.OwnerId; } // We don't want to do anything at all for textual stuff, let's also make sure we // are dealing with an entity (should always be the case) if (obj is MText || obj is DBText || !(obj is Entity)) { return; } // Now let's get the name of the layer, to use later Entity ent = (Entity)obj; LayerTableRecord ltr = (LayerTableRecord)tr.GetObject(ent.LayerId, OpenMode.ForRead); string layName = ltr.Name; // Clone the selected object object o = ent.Clone(); Entity clone = o as Entity; // We need to manipulate the clone to make sure it works if (clone != null) { // Setting the properties from the block reference helps certain entities get the // right references (and allows them to be offset properly) clone.SetPropertiesFrom(br); // But we then need to get the layer information from the database to set the // right layer (at least) on the new entity LayerTable lt = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForRead); if (lt.Has(layName)) { clone.LayerId = lt[layName]; } // Now we need to transform the entity for each of its Xref block reference containers // If we don't do this then entities in nested Xrefs may end up in the wrong place ObjectId[] conts = pner.GetContainers(); foreach (ObjectId contId in conts) { BlockReference cont = tr.GetObject(contId, OpenMode.ForRead) as BlockReference; if (cont != null) { clone.TransformBy(cont.BlockTransform); } } // Let's add the cloned entity to the current space BlockTableRecord space = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord; if (space == null) { clone.Dispose(); return; } ObjectId cloneId = space.AppendEntity(clone); tr.AddNewlyCreatedDBObject(clone, true); // Now let's flush the graphics, to help our clone get displayed tr.TransactionManager.QueueForGraphicsFlush(); // And we add our cloned entity to the list for deletion _ids.Add(cloneId); // Created a non-graphical selection of our newly created object and replace it with // the selection of the container Xref IntPtr ip = (IntPtr)0; SelectedObject so = new SelectedObject(cloneId, SelectionMethod.NonGraphical, ip); //????????????????????????????????? e.ReplaceSelectedObject(so); } } catch (System.Exception ex) { BaseObjs.writeDebug(ex.Message + " xRefOffsetApp.cs: line: 259"); } } } } } tr.Commit(); } } catch (System.Exception ex) { BaseObjs.writeDebug(ex.Message + " xRefOffsetApp.cs: line: 270"); } } }
getNestedEntity(string prompt, out bool escape, out Point3d pnt3dX, out string nameLayer, out FullSubentityPath path) { nameLayer = ""; Entity ent = null; Database db = BaseObjs._db; path = new FullSubentityPath(); Editor ed = BaseObjs._editor; PromptNestedEntityOptions pneo = new PromptNestedEntityOptions(prompt); PromptNestedEntityResult pner = ed.GetNestedEntity(pneo); if (pner.Status == PromptStatus.Cancel || pner.Status == PromptStatus.None) { pnt3dX = Pub.pnt3dO; escape = true; return(null); } pnt3dX = pner.PickedPoint; try { using (Transaction tr = BaseObjs.startTransactionDb()) { if (pner.Status == PromptStatus.OK) { escape = false; ObjectId[] idsContainers = pner.GetContainers(); ObjectId idSel = pner.ObjectId; int len = idsContainers.Length; ObjectId[] idsRev = new ObjectId[len + 1]; //Reverse the "containers" list for (int i = 0; i < len; i++) { ObjectId id = (ObjectId)idsContainers.GetValue(len - 1 - i); idsRev.SetValue(id, i); } //Now add the selected entity to the end of the array idsRev.SetValue(idSel, len); //Retrieve the sub-entity path for this entity SubentityId idSubEnt = new SubentityId(SubentityType.Null, (IntPtr)0); path = new FullSubentityPath(idsRev, idSubEnt); ObjectId idX = (ObjectId)idsRev.GetValue(0); ent = (Entity)tr.GetObject(idX, OpenMode.ForRead); ent.Highlight(path, false); DBObject obj = idSel.GetObject(OpenMode.ForRead); ObjectId idOwner = ObjectId.Null; if (obj is PolylineVertex3d || obj is Vertex2d) { idOwner = obj.OwnerId; ent = (Entity)tr.GetObject(idOwner, OpenMode.ForRead); } else if (obj is MText || obj is DBText || !(obj is Entity)) { return(ent); } else { ent = (Entity)obj; } object o = ent.Clone(); Entity clone = (Entity)o; if (clone != null) { //ObjectId[] conts = pner.GetContainers(); foreach (ObjectId idContainer in idsContainers) { BlockReference br = (BlockReference)idContainer.GetObject(OpenMode.ForRead); if (br != null) { clone.SetPropertiesFrom(br); clone.TransformBy(br.BlockTransform); } } BlockTableRecord space = (BlockTableRecord)db.CurrentSpaceId.GetObject(OpenMode.ForWrite); if (space == null) { clone.Dispose(); return(ent); } else if (clone is Arc) { Arc arc = (Arc)clone; pnt3dX = arc.GetClosestPointTo(pnt3dX, false); } else if (clone is Line) { Line line = (Line)clone; pnt3dX = line.GetClosestPointTo(pnt3dX, false); } else if (clone is Polyline) { Polyline poly = (Polyline)clone; pnt3dX = poly.GetClosestPointTo(pnt3dX, false); } else { Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(string.Format("Selected item was of type: {0}. Exiting...", clone.GetType().Name)); } nameLayer = ent.Layer.ToString(); tr.Commit(); return(ent); } } else { escape = true; } } } catch (System.Exception ex) { BaseObjs.writeDebug(ex.Message + " xRef.cs: line: 842"); escape = true; } return(ent); }
copyXRefEnts(ObjectId idBlkRef, string nameLayer, string nameLayerTarget) { ObjectIdCollection idEnts = getXRefEntsByLayer(idBlkRef, nameLayer); ObjectIdCollection ids = new ObjectIdCollection(); Layer.manageLayers(nameLayerTarget); Vector3d v3d = default(Vector3d); try { using (Transaction tr = BaseObjs.startTransactionDb()) { BlockReference br = (BlockReference)tr.GetObject(idBlkRef, OpenMode.ForRead); foreach (ObjectId idEnt in idEnts) { Entity ent = (Entity)tr.GetObject(idEnt, OpenMode.ForWrite); object o = ent.Clone(); Entity clone = (Entity)o; if (clone != null) { clone.SetPropertiesFrom(br); clone.Layer = nameLayerTarget; BlockTableRecord space = (BlockTableRecord)BaseObjs._db.CurrentSpaceId.GetObject(OpenMode.ForWrite); if (space == null) { clone.Dispose(); return(null); } if (o is Arc) { Arc a = (Arc)o; v3d = a.Normal; } else if (o is Line) { Line l = (Line)o; v3d = l.Normal; } else if (o is Polyline) { Polyline p = (Polyline)o; v3d = p.Normal; } // Translate the OCS to WCS Matrix3d mWPlane = Matrix3d.WorldToPlane(v3d); clone.TransformBy(mWPlane); space.AppendEntity(clone); tr.AddNewlyCreatedDBObject(clone, true); ids.Add(clone.ObjectId); } } tr.Commit(); } } catch (System.Exception ex) { BaseObjs.writeDebug(ex.Message + " xRef.cs: line: 122"); } return(ids); }
getNestedEntity(PromptEntityResult per, BlockReference br, bool makeCopy, out string nameLayer) { nameLayer = ""; Entity ent = null; Editor ed = BaseObjs._editor; Database db = BaseObjs._db; PromptNestedEntityOptions pneo = new PromptNestedEntityOptions(""); pneo.NonInteractivePickPoint = per.PickedPoint; pneo.UseNonInteractivePickPoint = true; PromptNestedEntityResult pner = ed.GetNestedEntity(pneo); try { using (Transaction tr = BaseObjs.startTransactionDb()) { if (pner.Status == PromptStatus.OK) { ObjectId idSel = pner.ObjectId; DBObject OBJ = idSel.GetObject(OpenMode.ForRead); if (OBJ is PolylineVertex3d || OBJ is Vertex2d) { idSel = OBJ.OwnerId; } if (OBJ is MText || OBJ is DBText || !(OBJ is Entity)) { return(null); } ent = (Entity)OBJ; if (makeCopy == true) { object o = ent.Clone(); Entity clone = o as Entity; if (clone != null) { clone.SetPropertiesFrom(br); ObjectId[] conts = pner.GetContainers(); foreach (ObjectId idCont in conts) { BlockReference cont = (BlockReference)idCont.GetObject(OpenMode.ForRead); if (cont != null) { clone.TransformBy(cont.BlockTransform); } } BlockTableRecord space = (BlockTableRecord)db.CurrentSpaceId.GetObject(OpenMode.ForWrite); if (space == null) { clone.Dispose(); nameLayer = ent.Layer; return(null); } space.AppendEntity(clone); tr.AddNewlyCreatedDBObject(clone, true); tr.Commit(); nameLayer = ent.Layer.ToString(); return(clone); } } else { return(ent); } } } } catch (System.Exception ex) { BaseObjs.writeDebug(ex.Message + " xRef.cs: line: 715"); } return(ent); }