Beispiel #1
0
        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);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Create a new UCS, make it active, and translate the coordinates of a point into the UCS coordinates
        /// https://knowledge.autodesk.com/search-result/caas/CloudHelp/cloudhelp/2017/ENU/AutoCAD-NET/files/GUID-096085E3-5AD5-4454-BF10-C9177FDB5979-htm.html
        /// </summary>
        public static void NewUCS()
        {
            // Get the current document and database, and start a transaction
            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();
            }
        }