예제 #1
0
        private static void AddText(Gem.Point2d pnt, string layer, string str)
        {
            App.Document acDoc   = App.Application.DocumentManager.MdiActiveDocument;
            Db.Database  acCurDb = acDoc.Database;

            // старт транзакции
            using (Db.Transaction acTrans = acCurDb.TransactionManager.StartOpenCloseTransaction())
            {
                // Открытие таблицы Блоков для чтения
                Db.BlockTable acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, Db.OpenMode.ForRead) as Db.BlockTable;

                // Открытие записи таблицы Блоков пространства Модели для записи
                Db.BlockTableRecord acBlkTblRec = acTrans.GetObject(acBlkTbl[Db.BlockTableRecord.ModelSpace],
                                                                    Db.OpenMode.ForWrite) as Db.BlockTableRecord;

                Db.MText acMText = new Db.MText();
                acMText.SetDatabaseDefaults();
                acMText.Location = new Gem.Point3d(pnt.X, pnt.Y, 0);
                acMText.Contents = str;
                acMText.Height   = SettingsParser.getInstance()._Scale.Coord;
                acMText.Color    = Autodesk.AutoCAD.Colors.
                                   Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByLayer, 256);
                acMText.Layer = layer;
                acMText.SetDatabaseDefaults();
                // Добавление нового объекта в запись таблицы блоков и в транзакцию
                acBlkTblRec.AppendEntity(acMText);
                acTrans.AddNewlyCreatedDBObject(acMText, true);
                // Сохранение нового объекта в базе данных
                acTrans.Commit();
            }
        }
예제 #2
0
        private static void AddCircle(Gem.Point2d pnt, string layer)
        {
            App.Document acDoc   = App.Application.DocumentManager.MdiActiveDocument;
            Db.Database  acCurDb = acDoc.Database;

            // старт транзакции
            using (Db.Transaction acTrans = acCurDb.TransactionManager.StartOpenCloseTransaction())
            {
                // Открытие таблицы Блоков для чтения
                Db.BlockTable acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                                                           Db.OpenMode.ForRead) as Db.BlockTable;

                // Открытие записи таблицы Блоков пространства Модели для записи
                Db.BlockTableRecord acBlkTblRec = acTrans.GetObject(acBlkTbl[Db.BlockTableRecord.ModelSpace],
                                                                    Db.OpenMode.ForWrite) as Db.BlockTableRecord;

                Db.Circle acCircle = new Db.Circle();
                acCircle.SetDatabaseDefaults();
                acCircle.Center = new Gem.Point3d(pnt.X, pnt.Y, 0);
                acCircle.Radius = SettingsParser.getInstance()._Scale.Circle;
                acCircle.Layer  = layer;
                // Добавление нового объекта в запись таблицы блоков и в транзакцию
                acBlkTblRec.AppendEntity(acCircle);
                acTrans.AddNewlyCreatedDBObject(acCircle, true);
                // Сохранение нового объекта в базе данных
                acTrans.Commit();
            }
        }
        public override List <string> GetObjectsInView()
        {
            var objs = new List <string>();

            using (AcadDb.Transaction tr = Doc.Database.TransactionManager.StartTransaction())
            {
                AcadDb.BlockTable       blckTbl     = tr.GetObject(Doc.Database.BlockTableId, AcadDb.OpenMode.ForRead) as AcadDb.BlockTable;
                AcadDb.BlockTableRecord blckTblRcrd = tr.GetObject(blckTbl[AcadDb.BlockTableRecord.ModelSpace], AcadDb.OpenMode.ForRead) as AcadDb.BlockTableRecord;
                foreach (AcadDb.ObjectId id in blckTblRcrd)
                {
                    var dbObj = tr.GetObject(id, AcadDb.OpenMode.ForRead);
                    if (dbObj is AcadDb.BlockReference)
                    {
                        var blckRef = (AcadDb.BlockReference)dbObj; // skip block references for now
                    }
                    else
                    {
                        objs.Add(dbObj.Handle.ToString());
                    }
                }
                // TODO: this returns all the doc objects. Need to check for visibility later.
                tr.Commit();
            }
            return(objs);
        }
예제 #4
0
        /// <summary>
        /// This is Extension Method for the <c>Autodesk.AutoCAD.DatabaseServices.LayoutManager</c>
        /// class. It gets the localized name of the Model tab.
        /// </summary>
        /// <param name="mng">Target <c>Autodesk.AutoCAD.DatabaseServices.LayoutManager</c>
        /// instance.</param>
        /// <returns>Returns the name of current space.</returns>
        public static String GetModelTabLocalizedName(this Db.LayoutManager mng)
        {
            Db.Database db = cad.DocumentManager.MdiActiveDocument.Database;
            String      modelTabLocalizedName = String.Empty;

            using (Db.Transaction tr = db.TransactionManager.StartTransaction())
            {
                Db.BlockTable       bt  = tr.GetObject(db.BlockTableId, Db.OpenMode.ForRead) as Db.BlockTable;
                Db.BlockTableRecord btr = tr.GetObject(bt[Db.BlockTableRecord.ModelSpace], Db.OpenMode.ForRead)
                                          as Db.BlockTableRecord;
                modelTabLocalizedName = (tr.GetObject(btr.LayoutId, Db.OpenMode.ForRead) as Db.Layout).LayoutName;
            }
            return(modelTabLocalizedName);
        }
예제 #5
0
        private static void AddMLeader(Gem.Point2d pnt, Gem.Vector3d otstup,
                                       double scale, string layer,
                                       string mleaderStyleName, string str)
        {
            SettingsParser settings = SettingsParser.getInstance();

            App.Document acDoc   = App.Application.DocumentManager.MdiActiveDocument;
            Db.Database  acCurDb = acDoc.Database;
            using (Db.Transaction acTrans = acCurDb.TransactionManager.StartOpenCloseTransaction())
            {
                Db.BlockTable acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                                                           Db.OpenMode.ForRead) as Db.BlockTable;
                Db.BlockTableRecord acBlkTblRec = acTrans.GetObject(acBlkTbl[Db.BlockTableRecord.ModelSpace],
                                                                    Db.OpenMode.ForWrite) as Db.BlockTableRecord;
                Db.MLeader acML = new Db.MLeader();
                acML.SetDatabaseDefaults();
                acML.Layer        = layer;
                acML.MLeaderStyle = CheckLocalRepository.GetIDbyName <Db.MLeaderStyle>(acCurDb, mleaderStyleName);
                acML.ContentType  = Db.ContentType.MTextContent;
                Db.MText mText = new Db.MText();
                mText.SetDatabaseDefaults();
                mText.Contents           = str;
                mText.TextHeight         = scale;
                mText.BackgroundFill     = settings.MTextMask;
                mText.UseBackgroundColor = settings.MTextMask;


                if (settings.MTextMask)
                {
                    mText.BackgroundScaleFactor = settings.MTextMaskKoefficient;
                }

                mText.Location = (new Gem.Point3d(pnt.X, pnt.Y, 0) + otstup);
                acML.MText     = mText;
                int idx = acML.AddLeaderLine(new Gem.Point3d(pnt.X, pnt.Y, 0));

                acML.Scale = 1;


                acBlkTblRec.AppendEntity(acML);
                acTrans.AddNewlyCreatedDBObject(acML, true);
                acTrans.Commit();
            }
        }
예제 #6
0
        /// <summary>
        /// This is Extension Method for the <c>Autodesk.AutoCAD.DatabaseServices.LayoutManager</c>
        /// class. It gets the name of the current space in the current Database.
        /// </summary>
        /// <param name="mng">Target <c>Autodesk.AutoCAD.DatabaseServices.LayoutManager</c>
        /// instance.</param>
        /// <returns>Returns the name of current space.</returns>
        public static String GetCurrentSpaceName(this Db.LayoutManager mng)
        {
            SpaceEnum space = GetCurrentSpaceEnum(mng);

            Db.Database db = cad.DocumentManager.MdiActiveDocument.Database;
            String      modelSpaceLocalizedName = String.Empty;

            using (Db.Transaction tr = db.TransactionManager.StartTransaction())
            {
                Db.BlockTable       bt  = tr.GetObject(db.BlockTableId, Db.OpenMode.ForRead) as Db.BlockTable;
                Db.BlockTableRecord btr = tr.GetObject(bt[Db.BlockTableRecord.ModelSpace], Db.OpenMode.ForRead)
                                          as Db.BlockTableRecord;
                modelSpaceLocalizedName = (tr.GetObject(btr.LayoutId, Db.OpenMode.ForRead) as Db.Layout).LayoutName;
            }
            String result = space == SpaceEnum.Viewport ?
                            "Model" as String : mng.CurrentLayout;

            return(result);
        }
예제 #7
0
        public override List <string> GetObjectsInView() // TODO: this returns all visible doc objects. handle views later.
        {
            var objs = new List <string>();

            using (AcadDb.Transaction tr = Doc.Database.TransactionManager.StartTransaction())
            {
                AcadDb.BlockTable       blckTbl     = tr.GetObject(Doc.Database.BlockTableId, AcadDb.OpenMode.ForRead) as AcadDb.BlockTable;
                AcadDb.BlockTableRecord blckTblRcrd = tr.GetObject(blckTbl[AcadDb.BlockTableRecord.ModelSpace], AcadDb.OpenMode.ForRead) as AcadDb.BlockTableRecord;
                foreach (AcadDb.ObjectId id in blckTblRcrd)
                {
                    var dbObj = tr.GetObject(id, AcadDb.OpenMode.ForRead);
                    if (dbObj.Visible())
                    {
                        objs.Add(dbObj.Handle.ToString());
                    }
                }
                tr.Commit();
            }
            return(objs);
        }
예제 #8
0
        //public  void GetLengedPoints(T model)
        //{
        //    Document doc = Application.DocumentManager.MdiActiveDocument;
        //    ObjectIdCollection ids = new ObjectIdCollection();

        //    PromptSelectionResult ProSset = null;
        //    TypedValue[] filList = new TypedValue[1] { new TypedValue((int)DxfCode.LayerName, MethodCommand.LegendLayer)};
        //    SelectionFilter sfilter = new SelectionFilter(filList);
        //    LayoutManager layoutMgr = LayoutManager.Current;

        //   string ss= layoutMgr.CurrentLayout;
        //    ProSset = doc.Editor.SelectAll(sfilter);
        ////  List<ObjectId> idss=  GetEntitiesInModelSpace();
        //    Database db = doc.Database;
        //    if (ProSset.Status == PromptStatus.OK)
        //    {
        //        using (Transaction tran = db.TransactionManager.StartTransaction())
        //        {
        //            SelectionSet sst = ProSset.Value;

        //            ObjectId[] oids = sst.GetObjectIds();
        //            model.LegendList = new Dictionary<int, List<System.Drawing.PointF>>();
        //            int ad = 0;
        //            List<string> aa = new List<string>();
        //            LayerModel lm = new LayerModel();
        //            LayerTable lt = (LayerTable)db.LayerTableId.GetObject(OpenMode.ForRead);
        //            foreach (ObjectId layerId in lt)
        //            {
        //                LayerTableRecord ltr = (LayerTableRecord)tran.GetObject(layerId, OpenMode.ForRead);
        //                if (ltr.Name == MethodCommand.LegendLayer)
        //                {
        //                    lm.Color =  System.Drawing.ColorTranslator.ToHtml(ltr.Color.ColorValue);
        //                }
        //            }
        //            for (int i = 0; i < oids.Length; i++)
        //            {
        //              //  if (idss.Contains(oids[i]))
        //              //  {
        //                    DBObject ob = tran.GetObject(oids[i], OpenMode.ForRead);
        //                if (!aa.Contains((ob as Polyline).BlockName)) { aa.Add((ob as Polyline).BlockName); }
        //                    if (ob is Polyline&&(ob as Polyline).BlockName.ToLower()== "*model_space")
        //                    {
        //                        lengedList.Add(ob as Polyline);
        //                        model.LegendList.Add(ad,PolylineMethod.GetPolyLineInfoPt(ob as Polyline));
        //                        ad++;
        //                    }
        //               // }


        //            }
        //            model.allLines = new List<LayerModel>();

        //            model.allLines.Add(lm);
        //        }
        //    }


        //}

        public List <Autodesk.AutoCAD.DatabaseServices.ObjectId> GetEntitiesInModelSpace()
        {
            List <Autodesk.AutoCAD.DatabaseServices.ObjectId> objects = new List <Autodesk.AutoCAD.DatabaseServices.ObjectId>();

            using (Autodesk.AutoCAD.DatabaseServices.Transaction transaction = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.TransactionManager.StartTransaction())
            {
                Autodesk.AutoCAD.DatabaseServices.BlockTable blockTable =
                    (Autodesk.AutoCAD.DatabaseServices.BlockTable)transaction.GetObject(
                        Autodesk.AutoCAD.DatabaseServices.HostApplicationServices.WorkingDatabase.BlockTableId,
                        Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead);
                Autodesk.AutoCAD.DatabaseServices.BlockTableRecord blockTableRecord =
                    (Autodesk.AutoCAD.DatabaseServices.BlockTableRecord)transaction.GetObject(
                        blockTable[Autodesk.AutoCAD.DatabaseServices.BlockTableRecord.ModelSpace],
                        Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead);
                foreach (Autodesk.AutoCAD.DatabaseServices.ObjectId objId in blockTableRecord)
                {
                    objects.Add(objId);
                }
                transaction.Commit();
            }
            return(objects);
        }
        /// <summary>
        /// insert signatures
        /// </summary>
        /// <param name="dwgPath"></param>
        /// <param name="dwgNameList"></param>
        public void InsertSign(string dwgPath, List <string> dwgNameList)
        {
            AdeskAppSvr.Document doc = AdeskAppSvr.Application.DocumentManager.MdiActiveDocument;
            this.Hide();
            AdeskDBSvr.Database         CurrDB          = doc.Database;//current database
            AdeskEdIn.Editor            ed              = doc.Editor;
            AdeskEdIn.PromptPointResult prPointRes_Base = ed.GetPoint(new AdeskEdIn.PromptPointOptions("选择插入的基点"));
            AdeskEdIn.PromptPointResult prPointRes_opp  = ed.GetPoint(new AdeskEdIn.PromptPointOptions("选择基点的对角点"));
            //In order to make the signs look nicely , calculate the base point and its opposite point
            AdeskGeo.Point3d P_base = CalPoint(prPointRes_Base.Value, prPointRes_opp.Value, 0.1);
            AdeskGeo.Point3d P_opp  = CalPoint(prPointRes_Base.Value, prPointRes_opp.Value, 0.9);
            //sign's width and height
            double SignWidth  = P_opp.X - P_base.X;
            double SignHeight = P_opp.Y - P_base.Y;
            //distance between each other
            double distanceW = prPointRes_opp.Value.X - prPointRes_Base.Value.X;
            double distanceH = prPointRes_opp.Value.Y - prPointRes_Base.Value.Y;
            //scale of signature. 36 is the width of sign, and 17 is the height. you should adjust them in your condition.
            double scaleWidth  = SignWidth / 36;
            double scaleHeight = SignHeight / 17;
            //today
            string date = System.DateTime.Today.ToLocalTime().ToString().Split(' ')[0];

            try
            {
                for (int i = 0; i < dwgNameList.Count; i++)
                {
                    using (AdeskDBSvr.Database tmpdb = new AdeskDBSvr.Database(false, false))
                    {
                        //read drawing
                        tmpdb.ReadDwgFile(dwgPath + dwgNameList[i], FileShare.Read, true, null);
                        //insert it as a new block
                        AdeskDBSvr.ObjectId idBTR = CurrDB.Insert(this.ICCardList[i], tmpdb, false);

                        using (AdeskDBSvr.Transaction trans = CurrDB.TransactionManager.StartTransaction())
                        {
                            AdeskDBSvr.BlockTable       bt  = (AdeskDBSvr.BlockTable)trans.GetObject(CurrDB.BlockTableId, AdeskDBSvr.OpenMode.ForRead);
                            AdeskDBSvr.BlockTableRecord btr = (AdeskDBSvr.BlockTableRecord)trans.GetObject(bt[AdeskDBSvr.BlockTableRecord.ModelSpace], AdeskDBSvr.OpenMode.ForWrite);
                            //insert point
                            AdeskGeo.Point3d inPt = new AdeskGeo.Point3d(P_base.X, P_base.Y - i * distanceH, P_base.Z);

                            #region signature date
                            //signature date
                            AdeskDBSvr.MText                SignDate       = new AdeskDBSvr.MText();
                            AdeskDBSvr.TextStyleTable       TextStyleTB    = (AdeskDBSvr.TextStyleTable)trans.GetObject(CurrDB.TextStyleTableId, AdeskDBSvr.OpenMode.ForWrite);
                            AdeskDBSvr.TextStyleTableRecord TextStyleTBRec = new AdeskDBSvr.TextStyleTableRecord();

                            TextStyleTBRec.Font = new AdeskGra.FontDescriptor("宋体", true, false, 0, 0);
                            TextStyleTB.Add(TextStyleTBRec);
                            trans.AddNewlyCreatedDBObject(TextStyleTBRec, true);
                            //trans.Commit();

                            SignDate.TextStyle  = TextStyleTBRec.Id;
                            SignDate.Contents   = date;
                            SignDate.TextHeight = SignHeight / 2;
                            SignDate.Width      = SignWidth / 3;
                            //date's location should fit the frame
                            SignDate.Location = new AdeskGeo.Point3d((inPt.X + distanceW), (inPt.Y + 1.5 * SignDate.TextHeight), inPt.Z);
                            btr.AppendEntity(SignDate);
                            trans.AddNewlyCreatedDBObject(SignDate, true);
                            #endregion

                            try
                            {
                                //create a ref to the block
                                using (AdeskDBSvr.BlockReference bref = new AdeskDBSvr.BlockReference(inPt, idBTR))
                                {
                                    bref.ScaleFactors = new AdeskGeo.Scale3d(scaleWidth, scaleHeight, 1.0);
                                    btr.AppendEntity(bref);
                                    trans.AddNewlyCreatedDBObject(bref, true);
                                }
                                trans.Commit();
                            }
                            catch (System.Exception err)
                            {
                                MessageBox.Show("one: " + err.Message);
                            }
                        }
                    }
                }
            }
            catch (Autodesk.AutoCAD.Runtime.Exception err)
            {
                MessageBox.Show("insert: " + err.Message);
            }
            this.Show();
        }
예제 #10
0
        void OpenDwgFromDatabaseLink(string fileName)
        {
            Document doc =
                Application.DocumentManager.MdiActiveDocument;


            if (!System.IO.File.Exists(fileName))
            {
                throw new FileNotFoundException(fileName);
            }

            // Create a database and try to load the file
            ACADDB.Database db = new ACADDB.Database(false, true);
            using (db)
            {
                try
                {
                    db.ReadDwgFile(
                        fileName,
                        System.IO.FileShare.Read,
                        false,
                        ""
                        );
                }
                catch (System.Exception)
                {
                    COMS.MessengerManager.AddLog(String.Format(
                                                     "Unable to read drawing file.",
                                                     db.FingerprintGuid.ToString())
                                                 );
                    return;
                }

                try
                {
                    ACADDB.Transaction tr =
                        db.TransactionManager.StartTransaction();
                    using (tr)
                    {
                        // Open the blocktable, get the modelspace
                        ACADDB.BlockTable bt =
                            (ACADDB.BlockTable)tr.GetObject(
                                db.BlockTableId,
                                ACADDB.OpenMode.ForRead
                                );

                        ACADDB.BlockTableRecord btr =
                            (ACADDB.BlockTableRecord)tr.GetObject(
                                bt[ACADDB.BlockTableRecord.ModelSpace],
                                ACADDB.OpenMode.ForRead
                                );


                        // Iterate through it, dumping objects
                        foreach (ACADDB.ObjectId objId in btr)
                        {
                            ACADDB.Entity ent =
                                (ACADDB.Entity)tr.GetObject(
                                    objId,
                                    ACADDB.OpenMode.ForRead
                                    );

                            // Let's get rid of the standard namespace
                            const string prefix =
                                "Autodesk.AutoCAD.DatabaseServices.";
                            string typeString =
                                ent.GetType().ToString();
                            if (typeString.Contains(prefix))
                            {
                                typeString =
                                    typeString.Substring(prefix.Length);
                            }

                            //ed.WriteMessage(
                            //    "\nEntity " +
                            //    ent.ObjectId.ToString() +
                            //    " of type " +
                            //    typeString +
                            //    " found on layer " +
                            //    ent.Layer +
                            //    " with colour " +
                            //    ent.Color.ToString()
                            //    );
                        }
                    }
                }
                catch (System.Exception)
                {
                    COMS.MessengerManager.AddLog(String.Format(
                                                     "Unable to read drawing file.",
                                                     db.FingerprintGuid.ToString())
                                                 );
                    return;
                }
            }
        }