/// <summary> /// List name of all UCS of active document /// </summary> /// <param name="db">Database of active document</param> /// <returns></returns> public static List <string> GetUCSName(Database db) { List <string> ucsNames = null; using (Transaction tr = db.TransactionManager.StartTransaction()) { // Open the UCS table for read UcsTable acUCSTbl; acUCSTbl = tr.GetObject(db.UcsTableId, OpenMode.ForRead) as UcsTable; // Listing foreach (ObjectId ucs in acUCSTbl) { ucsNames = new List <string>(); UcsTableRecord acUCSTblRec = (UcsTableRecord)tr.GetObject( ucs, OpenMode.ForRead); if (acUCSTblRec != null) { ucsNames.Add(acUCSTblRec.Name); } } tr.Commit(); } return(ucsNames); }
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); }
public static UcsTableRecord GetOrCreateUCS(Transaction trans, Database acCurDb, string ucsName) { UcsTable acUCSTbl; acUCSTbl = acCurDb.UcsTableId.GetObject(OpenMode.ForRead) as UcsTable; UcsTableRecord acUCSTblRec; // 检查UCS表中是否有“New_UCS”这条记录 if (acUCSTbl.Has(ucsName) == false) { acUCSTblRec = new UcsTableRecord(); acUCSTblRec.Name = ucsName; // 以写模式打开UCSTable acUCSTbl.UpgradeOpen(); // 往UCSTable添加新记录 acUCSTbl.Add(acUCSTblRec); trans.AddNewlyCreatedDBObject(acUCSTblRec, true); acUCSTbl.DowngradeOpen(); } else { acUCSTblRec = trans.GetObject(acUCSTbl[ucsName], OpenMode.ForRead) as UcsTableRecord; } return(acUCSTblRec); }
/// <summary> /// Transform point3D coordinate from WCS to named UCS /// </summary> /// <param name="pnts"></param> /// <param name="ucsName"></param> /// <returns></returns> public static Point3dCollection Wcs2Ucs(Point3dCollection pnts, string ucsName) { Document doc = acApp.DocumentManager.MdiActiveDocument; Database db = doc.Database; Point3dCollection ucsPnts = new Point3dCollection(); using (Transaction tr = db.TransactionManager.StartTransaction()) { UcsTable acUCSTbl; acUCSTbl = tr.GetObject(db.UcsTableId, OpenMode.ForRead) as UcsTable; UcsTableRecord ucs1 = tr.GetObject(acUCSTbl[ucsName], OpenMode.ForRead) as UcsTableRecord; Matrix3d mat = Matrix3d.AlignCoordinateSystem( ucs1.Origin, ucs1.XAxis, ucs1.YAxis, ucs1.XAxis.CrossProduct(ucs1.YAxis), Point3d.Origin, Vector3d.XAxis, Vector3d.YAxis, Vector3d.XAxis.CrossProduct(ucs1.YAxis) ); // Translate foreach (Point3d p in pnts) { ucsPnts.Add(p.TransformBy(mat)); } tr.Commit(); } return(ucsPnts); }
Stream(ArrayList data, UcsTableRecord rec) { data.Add(new Snoop.Data.ClassSeparator(typeof(UcsTableRecord))); data.Add(new Snoop.Data.Point3d("Origin", rec.Origin)); data.Add(new Snoop.Data.Vector3d("X axis", rec.XAxis)); data.Add(new Snoop.Data.Vector3d("Y axis", rec.YAxis)); }
public static UcsTableRecord CreateTempUtc(Point3d origin, Vector3d xAxis, Vector3d yAxis) { UcsTableRecord ucs = new UcsTableRecord(); ucs.Origin = origin; ucs.XAxis = xAxis; ucs.YAxis = yAxis; return(ucs); }
public void InsertUcsFromFileStart() { var pofo = new PromptOpenFileOptions("Select a file") { InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) }; var res = _ed.GetFileNameForOpen(pofo); if (res.Status != PromptStatus.OK) { return; } using (var loc = _doc.LockDocument()) { using (var destBaseTr = _db.TransactionManager.StartTransaction()) { var destUcsTable = (UcsTable)destBaseTr.GetObject(_db.UcsTableId, OpenMode.ForWrite); using (var sourceDb = new Database(false, true)) { sourceDb.ReadDwgFile(res.StringResult, FileOpenMode.OpenForReadAndAllShare, true, ""); using (var tr = sourceDb.TransactionManager.StartTransaction()) { var sourceUcsTable = (UcsTable)tr.GetObject(sourceDb.UcsTableId, OpenMode.ForRead); foreach (ObjectId uscRecordId in sourceUcsTable) { var ucsTr = (UcsTableRecord)tr.GetObject(uscRecordId, OpenMode.ForRead); var suffix = 1; var name = ucsTr.Name; while (destUcsTable.Has(name)) { _editorHelper.WriteMessage(String.Format("UCS {0} exists!\n", ucsTr.Name)); name = String.Format("{0}{1}", ucsTr.Name, String.Format("({0})", suffix)); suffix++; } var x = new UcsTableRecord { Name = name, Origin = ucsTr.Origin, XAxis = ucsTr.XAxis, YAxis = ucsTr.YAxis }; destUcsTable.Add(x); destBaseTr.AddNewlyCreatedDBObject(x, true); } } } destBaseTr.Commit(); } } }
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"); } }
/// <summary> /// 设置UCS的原点 /// </summary> /// <param name="ucsId">UCS的Id</param> /// <param name="pt">要设置的UCS原点坐标</param> public static void SetUCSOrigin(this ObjectId ucsId, Point3d pt) { Database db = ucsId.Database; var trans = db.TransactionManager; //打开UCS UcsTableRecord utr = trans.GetObject(ucsId, OpenMode.ForRead) as UcsTableRecord; if (utr == null) { return; //若UCS不存在,则返回 } utr.UpgradeOpen(); //切换UCS为写的状态 utr.Origin = pt; //设置UCS的原点 utr.DowngradeOpen(); //为了安全,切换UCS为读的状态 }
public void TestAddUcs() { var newId = ObjectId.Null; using (var db = AcadDatabase.Active()) { var newUcs = new UcsTableRecord() { Name = "NewUcs" }; db.Ucss.Add(newUcs); newId = newUcs.ObjectId; } AcadAssert.That.UcsTable.Contains(newId); }
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="ucsId">UCS的Id</param> /// <param name="rotateAngle">旋转角度</param> /// <param name="rotateAxis">旋转轴</param> public static void RotateUCS(this ObjectId ucsId, double rotateAngle, Vector3d rotateAxis) { Database db = ucsId.Database; var trans = db.TransactionManager; //打开UCS UcsTableRecord utr = trans.GetObject(ucsId, OpenMode.ForRead) as UcsTableRecord; if (utr == null) { return; //若UCS不存在,则返回 } utr.UpgradeOpen(); //切换UCS为写的状态 Vector3d xAxis = utr.XAxis; //UCS的X轴方向 Vector3d yAxis = utr.YAxis; //UCS的Y轴方向 //旋转UCS utr.XAxis = xAxis.RotateBy(rotateAngle * Math.PI / 180, rotateAxis); utr.YAxis = yAxis.RotateBy(rotateAngle * Math.PI / 180, rotateAxis); utr.DowngradeOpen();//为了安全,切换UCS为读的状态 }
/// <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 }
/// <summary> 旋转视口,并保持视口内容相对于视口的位置不变 </summary> /// <param name="vp">要进行旋转的视口</param> /// <param name="layout">视口所在的那个布局空间</param> /// <param name="basePt">旋转的基点在图纸空间中的坐标</param> /// <param name="angle">要旋转的角度,单位为弧度,按右手准则进行旋转</param> public static void RotateViewport(this Viewport vp, DocumentModifier docMdf, Layout layout, Point2d basePt, double angle) { ViewportTableRecord vpr; var ed = Application.DocumentManager.MdiActiveDocument.Editor; // 对视口所绑定的几何曲线 layoutClipCurve 的Rotation 操作可以对视口进行旋转,但是奇怪的是,在变换过程中,视口中的显示内容相对于布局空间未发生旋转,却进行了平移与缩放。 // 平移的后的视图中心点依然与视口的几何中心点重合,缩放的比例可以暂且简单理解为"1/cos(angle)"。 // 而如果要实现视口中内容随视口进行整体旋转,必须对acVport对象进行旋转变换。 vp.TransformBy(Matrix3d.Rotation(angle, axis: new Vector3d(0, 0, 1), center: new Point3d(0, 0, 0))); vp.UcsIconVisible = true; vp.UcsIconAtOrigin = true; Curve curve = null; if (vp.NonRectClipOn) { curve = vp.NonRectClipEntityId.GetObject(OpenMode.ForRead) as Curve; if (curve != null) { curve.UpgradeOpen(); docMdf.WriteNow("找到视口对应的多段线"); // // curve.TransformBy(Matrix3d.Rotation(angle, axis: new Vector3d(0, 0, 1), center: new Point3d(0, 0, 0))); } } // var ucsW = docMdf.acEditor.CurrentUserCoordinateSystem; Point3d ucsO = new Point3d(100, 100, 0); Vector3d ucsX = new Vector3d(1, 0, 0); Vector3d ucsY = new Vector3d(0, 1, 0); UcsTableRecord ucs = GetOrCreateUCS(docMdf.acTransaction, docMdf.acDataBase, "新ucs"); ucs.Origin = new Point3d(100, 100, 0);; ucs.XAxis = new Vector3d(1, 0, 0); ucs.YAxis = new Vector3d(0, 1, 0); vp.SetUcs(ucs.Id); // docMdf.acEditor.UpdateTiledViewportsFromDatabase(); vp.UcsFollowModeOn = true; docMdf.WriteNow($"结束. {vp.UcsName}"); }
getUcsTableRecord(string name) { try { using (Transaction tr = BaseObjs.startTransactionDb()) { UcsTable UcsT = getUcsTable(); if (UcsT.Has(name) == true) { UcsTableRecord Ucstr = (UcsTableRecord)tr.GetObject(UcsT[name], OpenMode.ForRead); return(Ucstr); } tr.Commit(); } } catch (System.Exception ex) { BaseObjs.writeDebug(ex.Message + " UCsys.cs: line: 272"); } return(null); }
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 CreateUCS() { Database db = HostApplicationServices.WorkingDatabase; Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; using (Transaction trans = db.TransactionManager.StartTransaction()) { UcsTable ut = (UcsTable)trans.GetObject(db.UcsTableId, OpenMode.ForWrite); String ucsName = "myUCS"; if (ut.Has(ucsName) == false) { UcsTableRecord utr = new UcsTableRecord(); utr.Name = ucsName; utr.Origin = new Point3d(0, 0, 0); utr.XAxis = new Vector3d(0, 1, 0); utr.YAxis = new Vector3d(-1, 0, 0); Matrix3d mt = Matrix3d.AlignCoordinateSystem(Point3d.Origin, Vector3d.XAxis, Vector3d.YAxis, Vector3d.ZAxis, utr.Origin, utr.XAxis, utr.YAxis, utr.XAxis.CrossProduct(utr.YAxis)); ed.CurrentUserCoordinateSystem = mt; } trans.Commit(); } }
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(); } }
// 开始具体的调试操作 private void DoSomething(DocumentModifier docMdf, SelectionSet impliedSelection) { var acTrans = docMdf.acTransaction; // 以读模式打开UCSTable UcsTable acUCSTbl; acUCSTbl = acTrans.GetObject(docMdf.acDataBase.UcsTableId, OpenMode.ForRead) as UcsTable; UcsTableRecord acUCSTblRec; // 检查UCS表中是否有“New_UCS”这条记录 if (acUCSTbl.Has("New_UCS") == false) { acUCSTblRec = new UcsTableRecord(); acUCSTblRec.Name = "New_UCS"; // 以写模式打开UCSTable acUCSTbl.UpgradeOpen(); // 往UCSTable添加新记录 acUCSTbl.Add(acUCSTblRec); acTrans.AddNewlyCreatedDBObject(acUCSTblRec, true); } else { acUCSTblRec = acTrans.GetObject(acUCSTbl["New_UCS"], OpenMode.ForWrite) as UcsTableRecord; } acUCSTblRec.Origin = new Point3d(4, 5, 3); acUCSTblRec.XAxis = new Vector3d(1, 0, 0); acUCSTblRec.YAxis = new Vector3d(0, 1, 0); // 打开当前视口 ViewportTableRecord acVportTblRec = acTrans.GetObject(docMdf.acEditor.ActiveViewportId, OpenMode.ForWrite) as ViewportTableRecord; // 在当前视口的原点显示UCS图标 acVportTblRec.IconAtOrigin = true; acVportTblRec.IconEnabled = true; // 设置UCS为当前坐标系 acVportTblRec.SetUcs(acUCSTblRec.ObjectId); docMdf.acEditor.UpdateTiledViewportsFromDatabase(); // 显示当前UCS坐标系的名称 UcsTableRecord acUCSTblRecActive = acTrans.GetObject(acVportTblRec.UcsName, OpenMode.ForRead) as UcsTableRecord; Application.ShowAlertDialog("The current UCS is: " + acUCSTblRecActive.Name); // 另一种方式显示出当前用户坐标系的参数 Matrix3d curUCSMatrix = docMdf.acEditor.CurrentUserCoordinateSystem; CoordinateSystem3d curUCS = curUCSMatrix.CoordinateSystem3d; // 在用户坐标系下绘制一条线 var btrTable = acTrans.GetObject(docMdf.acDataBase.BlockTableId, OpenMode.ForRead) as BlockTable; var btr = acTrans.GetObject(btrTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; var line = new Line(new Point3d(0, 0, 0), new Point3d(1, 0, 0)); // Entity.TransformBy() 用来进行坐标变换,如果不进行变换,则上面的line的定位是相对于世界坐标系的。 line.TransformBy(curUCSMatrix); btr.AppendEntity(line); acTrans.AddNewlyCreatedDBObject(line, true); // 提示选取一个点 var pPtOpts = new PromptPointOptions(""); pPtOpts.Message = "\nEnter a point: "; var pPtRes = docMdf.acEditor.GetPoint(pPtOpts); if (pPtRes.Status == PromptStatus.OK) { // 获得的点的坐标是在当前UCS下定义的 var pt3dUCS = pPtRes.Value; // 将该点的当前UCS坐标转变为WCS坐标 var newMatrix = Matrix3d.AlignCoordinateSystem(Point3d.Origin, Vector3d.XAxis, Vector3d.YAxis, Vector3d.ZAxis, acVportTblRec.Ucs.Origin, acVportTblRec.Ucs.Xaxis, acVportTblRec.Ucs.Yaxis, acVportTblRec.Ucs.Zaxis); var pt3dWCS = pt3dUCS.TransformBy(newMatrix); Application.ShowAlertDialog("The WCS coordinates are: \n" + pt3dWCS.ToString() + "\n" + "The UCS coordinates are: \n" + pt3dUCS.ToString()); } }
public static UcsTableRecord CreateUcs(Point3d aOrigin, Point3d aXAxis, Point3d aYAxis, string aName) { //bool UpdateViewport = false; var doc = Application.DocumentManager.MdiActiveDocument; var ed = doc.Editor; var db = doc.Database; var dblRotation = GetAngleFromXAxis(aOrigin, aXAxis); //*Math.PI / 180; var xAxis = new Vector3d(1 * Math.Cos(dblRotation), 1 * Math.Sin(dblRotation), 0); var yAxis = new Vector3d(1 * Math.Sin(-1 * dblRotation), 1 * Math.Cos(dblRotation), 0); using (Application.DocumentManager.MdiActiveDocument.LockDocument()) { try { UcsTableRecord ucstr; using (var tr = db.TransactionManager.StartTransaction()) { using (var ucst = (UcsTable)tr.GetObject(db.UcsTableId, OpenMode.ForWrite)) { if (!ucst.Has(aName)) { ucstr = new UcsTableRecord { Name = aName }; ucst.UpgradeOpen(); ucst.Add(ucstr); tr.AddNewlyCreatedDBObject(ucstr, true); } else { ucstr = (UcsTableRecord)tr.GetObject(ucst[aName], OpenMode.ForWrite); } ucstr.Origin = aOrigin; ucstr.XAxis = xAxis; ucstr.YAxis = yAxis; /* * if (UpdateViewport) * { * //Debug.Print(Doc.Editor.CurrentViewportObjectId.ToString); * ViewportTableRecord vport = (ViewportTableRecord)Tr.GetObject(Doc.Editor.CurrentViewportObjectId, OpenMode.ForWrite); * vport.SetUcs(Ucstr.ObjectId); * Doc.Editor.UpdateTiledViewportsFromDatabase(); * } */ tr.Commit(); } } return(ucstr); } catch (Exception ex) { ed.WriteMessage(ex.Message + "\n" + ex.StackTrace); return(new UcsTableRecord()); } } }
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); }
Stream(ArrayList data, AcDb.SymbolTableRecord tblRec) { data.Add(new Snoop.Data.ClassSeparator(typeof(AcDb.SymbolTableRecord))); data.Add(new Snoop.Data.String("Name", tblRec.Name)); data.Add(new Snoop.Data.Bool("Is dependent", tblRec.IsDependent)); data.Add(new Snoop.Data.Bool("Is resolved", tblRec.IsResolved)); // branch to all known major sub-classes AbstractViewTableRecord viewRec = tblRec as AbstractViewTableRecord; if (viewRec != null) { Stream(data, viewRec); return; } BlockTableRecord blkRec = tblRec as BlockTableRecord; if (blkRec != null) { Stream(data, blkRec); return; } DimStyleTableRecord dimRec = tblRec as DimStyleTableRecord; if (dimRec != null) { Stream(data, dimRec); return; } LayerTableRecord layRec = tblRec as LayerTableRecord; if (layRec != null) { Stream(data, layRec); return; } LinetypeTableRecord ltypeRec = tblRec as LinetypeTableRecord; if (ltypeRec != null) { Stream(data, ltypeRec); return; } TextStyleTableRecord textRec = tblRec as TextStyleTableRecord; if (textRec != null) { Stream(data, textRec); return; } UcsTableRecord ucsRec = tblRec as UcsTableRecord; if (ucsRec != null) { Stream(data, ucsRec); return; } }
public static void NewUCS() { Editor ed = Application.DocumentManager.CurrentDocument.Editor; // Get the current document and database, and start a transaction try { Document acDoc = Application.DocumentManager.MdiActiveDocument; Database acCurDb = acDoc.Database; using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) { // Open the UCS table for read UcsTable acUCSTbl; acUCSTbl = acTrans.GetObject(acCurDb.UcsTableId, OpenMode.ForRead) as UcsTable; UcsTableRecord acUCSTblRec; // Check to see if the "New_UCS" UCS table record exists if (acUCSTbl.Has("New_UCS") == false) { acUCSTblRec = new UcsTableRecord(); acUCSTblRec.Name = "New_UCS"; // Open the UCSTable for write acUCSTbl.UpgradeOpen(); // Add the new UCS table record acUCSTbl.Add(acUCSTblRec); acTrans.AddNewlyCreatedDBObject(acUCSTblRec, true); //acUCSTblRec.Dispose(); } else { acUCSTblRec = acTrans.GetObject(acUCSTbl["New_UCS"], OpenMode.ForWrite) as UcsTableRecord; } acUCSTblRec.Origin = new Point3d(4, 5, 3); acUCSTblRec.XAxis = new Vector3d(1, 0, 0); acUCSTblRec.YAxis = new Vector3d(0, 1, 0); // Open the active viewport ViewportTableRecord acVportTblRec; acVportTblRec = acTrans.GetObject(acDoc.Editor.ActiveViewportId, OpenMode.ForWrite) as ViewportTableRecord; // Display the UCS Icon at the origin of the current viewport acVportTblRec.IconAtOrigin = true; acVportTblRec.IconEnabled = true; // Set the UCS current acVportTblRec.SetUcs(acUCSTblRec.ObjectId); acDoc.Editor.UpdateTiledViewportsFromDatabase(); // Display the name of the current UCS UcsTableRecord acUCSTblRecActive; acUCSTblRecActive = acTrans.GetObject(acVportTblRec.UcsName, OpenMode.ForRead) as UcsTableRecord; Application.ShowAlertDialog("The current UCS is: " + acUCSTblRecActive.Name); PromptPointResult pPtRes; PromptPointOptions pPtOpts = new PromptPointOptions(""); // Prompt for a point pPtOpts.Message = "\nEnter a point: "; pPtRes = acDoc.Editor.GetPoint(pPtOpts); Point3d pPt3dWCS; Point3d pPt3dUCS; // If a point was entered, then translate it to the current UCS if (pPtRes.Status == PromptStatus.OK) { pPt3dWCS = pPtRes.Value; pPt3dUCS = pPtRes.Value; // Translate the point from the current UCS to the WCS Matrix3d newMatrix = new Matrix3d(); newMatrix = Matrix3d.AlignCoordinateSystem(Point3d.Origin, Vector3d.XAxis, Vector3d.YAxis, Vector3d.ZAxis, acVportTblRec.Ucs.Origin, acVportTblRec.Ucs.Xaxis, acVportTblRec.Ucs.Yaxis, acVportTblRec.Ucs.Zaxis); pPt3dWCS = pPt3dWCS.TransformBy(newMatrix); Application.ShowAlertDialog("The WCS coordinates are: \n" + pPt3dWCS.ToString() + "\n" + "The UCS coordinates are: \n" + pPt3dUCS.ToString()); } // Save the new objects to the database acTrans.Commit(); } } catch (System.Exception ex) { ed.WriteMessage("Error " + ex.Message); } }
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); } } }