addUcsTableRecord(string name) { ObjectId id = ObjectId.Null; UcsTableRecord Ucstr = new UcsTableRecord(); try { using (Transaction tr = BaseObjs.startTransactionDb()) { UcsTable UcsT = getUcsTable(); try { Ucstr.Name = name; UcsT.Add(Ucstr); tr.AddNewlyCreatedDBObject(Ucstr, true); } catch (System.Exception ex) { BaseObjs.writeDebug(ex.Message + " UCsys.cs: line: 177"); } tr.Commit(); id = Ucstr.ObjectId; } } catch (System.Exception ex) { BaseObjs.writeDebug(ex.Message + " UCsys.cs: line: 185"); } return(id); }
setUCS2Object(Point3d pnt3dBase0, Point3d pnt3dBaseX, Point3d pnt3dBaseY) { Vector3d vec3dXaxis = new Vector3d(pnt3dBaseX.X - pnt3dBase0.X, pnt3dBaseX.Y - pnt3dBase0.Y, pnt3dBaseX.Z - pnt3dBase0.Z); Vector3d vec3dYaxis = new Vector3d(pnt3dBaseY.X - pnt3dBase0.X, pnt3dBaseY.Y - pnt3dBase0.Y, pnt3dBaseY.Z - pnt3dBase0.Z); try { using (Transaction tr = BaseObjs.startTransactionDb()) { // Open the UCS table for read UcsTable acUCSTbl = getUcsTable(); UcsTableRecord acUCSTblRec; // Check to see if the "New_UCS" UCS table record exists if (acUCSTbl.Has("Temp") == false) { acUCSTblRec = new UcsTableRecord(); acUCSTblRec.Name = "Temp"; // Open the UCSTable for write acUCSTbl.UpgradeOpen(); // Add the new UCS table record acUCSTbl.Add(acUCSTblRec); tr.AddNewlyCreatedDBObject(acUCSTblRec, true); } else { acUCSTblRec = tr.GetObject(acUCSTbl["Temp"], OpenMode.ForWrite) as UcsTableRecord; } acUCSTblRec.Origin = pnt3dBase0; acUCSTblRec.XAxis = vec3dXaxis; acUCSTblRec.YAxis = vec3dYaxis; // Open the active viewport ViewportTableRecord acVportTblRec; acVportTblRec = tr.GetObject(BaseObjs._editor.ActiveViewportId, OpenMode.ForWrite) as ViewportTableRecord; // Set the UCS current acVportTblRec.SetUcs(acUCSTblRec.ObjectId); BaseObjs._editor.UpdateTiledViewportsFromDatabase(); tr.Commit(); } } catch (System.Exception ex) { BaseObjs.writeDebug(ex.Message + " UCsys.cs: line: 328"); } }
public void Test() { Document document = Application.DocumentManager.MdiActiveDocument; using (Transaction trans = document.TransactionManager.StartTransaction()) { UcsTable ut = trans.GetObject(document.Database.UcsTableId, OpenMode.ForRead) as UcsTable; UcsTableRecord utr = trans.GetObject(ut["TestUcs"], OpenMode.ForRead); utr.Name = "TestUcs"; utr.Origin = basePt; utr.XAxis = vecX; utr.XAxis = vecY; ut.Add(utr); trans.AddNewlyCreatedDBObject(utr, true); trans.Commit(); } }
/// <summary> /// 创建一个新的UCS /// </summary> /// <param name="db">数据库对象</param> /// <param name="UCSName">要创建的UCS名称</param> /// <returns>返回创建的UCS的Id</returns> public static ObjectId AddUCS(this Database db, string UCSName) { var trans = db.TransactionManager; //打开UCS表 UcsTable ut = (UcsTable)trans.GetObject(db.UcsTableId, OpenMode.ForRead); if (!ut.Has(UCSName))//如果不存在名为UCSName的UCS,则新建一个UCS { //定义一个新的UCS UcsTableRecord utr = new UcsTableRecord(); utr.Name = UCSName; //设置UCS名 ut.UpgradeOpen(); //切换UCS表的状态为写以添加新的UCS //将UCS的信息添加到UCS表中 ut.Add(utr); //把UCS添加到事务处理中 trans.AddNewlyCreatedDBObject(utr, true); ut.DowngradeOpen(); //为了安全,将UCS表的状态切换为读 } return(ut[UCSName]); //返回新添加的UCS的ObjectId }
public static ObjectId OpenOrCreateUserCoordinateSystem(string ucsName, bool openIfExists = true) { using (Transaction trans = Tools.StartTransaction()) { // Open the UCS table for read UcsTable ucsTable = (UcsTable)trans.GetObject(Tools.GetAcadDatabase().UcsTableId, OpenMode.ForRead); UcsTableRecord ucsTblRec; // Check to see if the "New_UCS" UCS table record exists if (ucsTable.Has(ucsName) == false) { ucsTblRec = new UcsTableRecord(); ucsTblRec.Name = ucsName; // Open the UCSTable for write ucsTable.UpgradeOpen(); // Add the new UCS table record ucsTable.Add(ucsTblRec); trans.AddNewlyCreatedDBObject(ucsTblRec, true); ucsTblRec.Dispose(); } else { if (openIfExists) { ucsTblRec = trans.GetObject(ucsTable[ucsName], OpenMode.ForWrite) as UcsTableRecord; } else { throw new ArgumentException(string.Format("\nA UCS with this name \"{0}\" alredy exists", ucsName)); } } return(ucsTblRec.Id); } }
public void Test() { Document document = Application.DocumentManager.MdiActiveDocument; Editor ed = document.Editor; Matrix3d mt = ed.CurrentUserCoordinateSystem; Point3d basePt = new Point3d(1000, 1000, 0); Vector3d vecX = new Vector3d(100, 100, 0); Vector3d vecY = vecX.GetPerpendicularVector(); using (Transaction trans = document.TransactionManager.StartTransaction()) { UcsTable ut = trans.GetObject(document.Database.UcsTableId, OpenMode.ForRead) as UcsTable; UcsTableRecord utr = new UcsTableRecord(); utr.Name = "TestUcs"; utr.Origin = basePt; utr.XAxis = vecX; utr.XAxis = vecY; ut.UpgradeOpen(); ut.Add(utr); ut.DowngradeOpen(); trans.AddNewlyCreatedDBObject(utr, true); trans.Commit(); } }
addUCS(Point3d pnt3dOrigin, Point3d pnt3dXaxis, Point3d pnt3dYaxis, string nameUCS) { Matrix3d newMatrix = new Matrix3d(); try { using (Transaction tr = BaseObjs.startTransactionDb()) { // Open the UCS table for read UcsTable ucsT = tr.GetObject(BaseObjs._db.UcsTableId, OpenMode.ForRead) as UcsTable; UcsTableRecord ucstr; Vector3d vector3dXaxis = new Vector3d(pnt3dXaxis.X - pnt3dOrigin.X, pnt3dXaxis.Y - pnt3dOrigin.Y, pnt3dXaxis.Z - pnt3dOrigin.Z); Vector3d vector3dYaxis = new Vector3d(pnt3dYaxis.X - pnt3dOrigin.X, pnt3dYaxis.Y - pnt3dOrigin.Y, pnt3dYaxis.Z - pnt3dOrigin.Z); Vector3d vector3dZ = vector3dXaxis.CrossProduct(vector3dYaxis); Vector3d vector3dY = vector3dZ.CrossProduct(vector3dXaxis); Vector3d vector3dPerpY = new Vector3d(vector3dY.X + pnt3dOrigin.X, vector3dY.Y + pnt3dOrigin.Y, vector3dY.Z + pnt3dOrigin.Z); // Check to see if the nameUCS UCS table record exists if (ucsT.Has(nameUCS) == false) { ucstr = new UcsTableRecord(); ucstr.Name = nameUCS; // Open the UCSTable for write ucsT.UpgradeOpen(); // Add the new UCS table record ucsT.Add(ucstr); tr.AddNewlyCreatedDBObject(ucstr, true); } else { ucstr = (UcsTableRecord)tr.GetObject(ucsT[nameUCS], OpenMode.ForWrite); } ucstr.Origin = pnt3dOrigin; ucstr.XAxis = vector3dXaxis; ucstr.YAxis = vector3dPerpY; // Open the active viewport ViewportTableRecord vptr; vptr = (ViewportTableRecord)tr.GetObject(BaseObjs._editor.ActiveViewportId, OpenMode.ForWrite); // Display the UCS Icon at the origin of the current viewport vptr.IconAtOrigin = true; vptr.IconEnabled = true; // Set the UCS current vptr.SetUcs(ucstr.ObjectId); BaseObjs._editor.UpdateTiledViewportsFromDatabase(); newMatrix = Matrix3d.AlignCoordinateSystem(Point3d.Origin, Vector3d.XAxis, Vector3d.YAxis, Vector3d.ZAxis, vptr.Ucs.Origin, vptr.Ucs.Xaxis, vptr.Ucs.Yaxis, vptr.Ucs.Zaxis); tr.Commit(); } } catch (System.Exception ex) { BaseObjs.writeDebug(ex.Message + " UCsys.cs: line: 149"); } return(newMatrix); }
private static bool SwitchUCS(Point3d cornerCoords, double textAngle) { // Get the current document and database Document acDoc = Application.DocumentManager.MdiActiveDocument; Database acCurrDb = acDoc.Database; Editor acEditor = acDoc.Editor; using (Transaction acTrans = acCurrDb.TransactionManager.StartTransaction()) { try { UcsTable acUCSTbl = acTrans.GetObject(acCurrDb.UcsTableId, OpenMode.ForRead) as UcsTable; UcsTableRecord jppUCSTblRec; // Check to see if JPP App UCS table record exists and create if not if (acUCSTbl.Has("JPP_App_UCS") == false) { jppUCSTblRec = new UcsTableRecord(); jppUCSTblRec.Name = "JPP_App_UCS"; acUCSTbl.UpgradeOpen(); acUCSTbl.Add(jppUCSTblRec); acTrans.AddNewlyCreatedDBObject(jppUCSTblRec, true); } else { jppUCSTblRec = acTrans.GetObject(acUCSTbl["JPP_App_UCS"], OpenMode.ForWrite) as UcsTableRecord; } jppUCSTblRec.Origin = cornerCoords; jppUCSTblRec.XAxis = cornerCoords.GetVectorTo(new Point3d(cornerCoords.X + Math.Cos(textAngle), cornerCoords.Y + Math.Sin(textAngle), 0.0)); jppUCSTblRec.YAxis = cornerCoords.GetVectorTo(new Point3d(cornerCoords.X - Math.Sin(textAngle), cornerCoords.Y + Math.Cos(textAngle), 0.0)); // Open the active viewport ViewportTableRecord acVportTblRec = acTrans.GetObject(acEditor.ActiveViewportId, OpenMode.ForWrite) as ViewportTableRecord; // Display the UCS Icon as the origin of the current viewport // acVportTblRec.IconAtOrigin = true; // acVportTblRec.IconEnabled = true; // Set the UCS current acVportTblRec.SetUcs(jppUCSTblRec.ObjectId); acEditor.UpdateTiledViewportsFromDatabase(); // Display the name of the current UCS UcsTableRecord acUCSTblRecActive = acTrans.GetObject(acVportTblRec.UcsName, OpenMode.ForRead) as UcsTableRecord; acEditor.WriteMessage("\nThe current UCS is: {0}", acUCSTblRecActive.Name); // Save the new objects to the database acTrans.Commit(); return(true); } catch (Autodesk.AutoCAD.Runtime.Exception acException) { Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog ("The following exception was caught: \n" + acException.Message + "\nError creating UCS!"); acTrans.Abort(); return(false); } } }