Exemple #1
0
        public void addXref()
        {
            using (var _form = new AddXrefForm())
            {
                _form.SetTab(0);

                var result = _form.ShowDialog();

                if (result == System.Windows.Forms.DialogResult.OK)
                {
                    xRefFile    = _form.xrefFile;
                    drawingList = _form.AddXrefDrawingList;
                }
                if (result == System.Windows.Forms.DialogResult.None)
                {
                    return;
                }
            }

            Document Doc = Application.DocumentManager.MdiActiveDocument;

            foreach (var drawing in drawingList)
            {
                Database xrefDb = new Database(false, true);
                xrefDb.ReadDwgFile(drawing, FileShare.ReadWrite, false, "");

                using (Transaction trx = xrefDb.TransactionManager.StartTransaction())
                {
                    xrefDb = createLayer(trx, xrefDb);

                    BlockTable       xrefBt = xrefDb.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable;
                    BlockTableRecord btrMs  = xrefBt[BlockTableRecord.ModelSpace].GetObject(OpenMode.ForWrite) as BlockTableRecord;

                    ObjectId xrefObjId = xrefDb.OverlayXref(xRefFile, Path.GetFileNameWithoutExtension(xRefFile));

                    BlockReference bref = new BlockReference(Point3d.Origin, xrefObjId);

                    btrMs.AppendEntity(bref);
                    trx.AddNewlyCreatedDBObject(bref, true);

                    xrefDb.CloseInput(true);
                    xrefDb.SaveAs(drawing, false, DwgVersion.Current, null);

                    trx.Commit();
                }
            }

            Doc.Editor.WriteMessage("Filene er xrefet\n");
        }
        public void AddXref_test()
        {
            string xRefFile;
            var    drawingList = new List <string>();

            using (var _form = new AddXrefForm())
            {
                var result = _form.ShowDialog();

                if (result == DialogResult.OK)
                {
                    xRefFile    = _form.xrefFile;
                    drawingList = _form.AddXrefDrawingList;
                }
                if (result == DialogResult.None)
                {
                    return;
                }

                Assert.AreEqual(DialogResult.OK, result);
            }
        }
        public void detachXref()
        {
            var DrawingList = new List <string>();

            using (var _form = new AddXrefForm())
            {
                _form.SetTab(2);

                var result = _form.ShowDialog();

                if (result == System.Windows.Forms.DialogResult.OK)
                {
                    DrawingList = _form.DetachXrefDrawingList;
                }
                if (result == System.Windows.Forms.DialogResult.None)
                {
                    return;
                }
            }

            //Get the document
            Document Doc = Application.DocumentManager.MdiActiveDocument;
            Editor   ed  = Doc.Editor;

            foreach (var drawing in DrawingList)
            {
                //create a database and try to load the file
                Database db = new Database(false, true);
                using (db)
                {
                    try
                    {
                        db.ReadDwgFile(drawing, System.IO.FileShare.ReadWrite, false, "");
                    }
                    catch (System.Exception)
                    {
                        ed.WriteMessage("\nUnable to read the drawingfile.");
                        return;
                    }
                    db.ResolveXrefs(true, false);
                    using (Transaction tr = db.TransactionManager.StartTransaction())
                    {
                        XrefGraph xg        = db.GetHostDwgXrefGraph(true);
                        int       xrefcount = xg.NumNodes;
                        for (int j = 0; j < xrefcount; j++)
                        {
                            XrefGraphNode xrNode   = xg.GetXrefNode(j);
                            String        nodeName = xrNode.Name;
                            if (xrNode.XrefStatus == XrefStatus.FileNotFound)
                            {
                                ObjectId detachid = xrNode.BlockTableRecordId;
                                db.DetachXref(detachid);
                                ed.WriteMessage("\nDetached successfully\n");
                                break;
                            }
                        }
                        tr.Commit();
                    }
                    // Overwrite the current drawing file with new updated XRef paths
                    try
                    {
                        db.SaveAs(drawing, false, DwgVersion.Current, null);
                    }
                    catch (System.Exception)
                    {
                        continue;
                    }
                }
            }

            Doc.Editor.WriteMessage("Xref is detached");
        }
        public void unloadXref()
        {
            var DrawingList = new List <string>();

            using (var _form = new AddXrefForm())
            {
                _form.SetTab(1);

                var result = _form.ShowDialog();

                if (result == System.Windows.Forms.DialogResult.OK)
                {
                    DrawingList = _form.UnloadXrefDrawingList;
                }
                if (result == System.Windows.Forms.DialogResult.None)
                {
                    return;
                }
            }

            //Get the document
            Document Doc = Application.DocumentManager.MdiActiveDocument;
            Editor   ed  = Doc.Editor;

            foreach (var drawing in DrawingList)
            {
                //create a database and try to load the file
                Database db = new Database(false, true);
                using (db)
                {
                    try
                    {
                        db.ReadDwgFile(drawing, FileShare.ReadWrite, false, "");
                    }
                    catch (System.Exception)
                    {
                        ed.WriteMessage("\nUnable to read the drawingfile.");
                        continue;
                    }
                    using (Transaction tr = db.TransactionManager.StartTransaction())
                    {
                        XrefGraph xg        = db.GetHostDwgXrefGraph(true);
                        int       xrefcount = xg.NumNodes - 1;

                        if (xrefcount != 0)
                        {
                            ObjectIdCollection XrefColl = new ObjectIdCollection();

                            for (int r = 1; r < (xrefcount + 1); r++)
                            {
                                XrefGraphNode xrefNode = xg.GetXrefNode(r);

                                ObjectId xrefId = xrefNode.BlockTableRecordId;
                                XrefColl.Add(xrefId);
                            }
                            db.UnloadXrefs(XrefColl);
                            tr.Commit();
                        }
                    }
                    // Overwrite the current drawing file with new updated XRef paths
                    db.SaveAs(drawing, false, DwgVersion.Current, null);
                }
            }

            Doc.Editor.WriteMessage("Xrefs unloaded\n");
        }