Esempio n. 1
0
        public static void Plan2ImportLayerFilters()
        {
            var filePath = GetDwgName();

            if (string.IsNullOrEmpty(filePath))
            {
                return;
            }

            if (!System.IO.File.Exists(filePath))
            {
                return;
            }

            _AcAp.Document        doc    = _AcAp.Application.DocumentManager.MdiActiveDocument;
            _AcEd.Editor          ed     = doc.Editor;
            _AcDb.Database        destDb = doc.Database;
            _AcLm.LayerFilterTree lft    = destDb.LayerFilters;
            using (_AcDb.Database srcDb = new _AcDb.Database(false, false))
            {
                srcDb.ReadDwgFile(filePath, _AcDb.FileOpenMode.OpenForReadAndAllShare, false, String.Empty);
                ImportNestedFilters(srcDb.LayerFilters.Root, lft.Root, srcDb, destDb, false, ed);
            }
            destDb.LayerFilters = lft;
        }
Esempio n. 2
0
        private bool getBlockFromMaster(ref List <string> missingBlocks)
        {
            _Db.Database sourceDb       = new _Db.Database(false, true);
            string       sourceFileName = @"C:\Brics_pealeehitus\master.dwg";

            if (!File.Exists(sourceFileName))
            {
                sourceFileName = @"C:\Users\aleksandr.ess\Dropbox\DMT\Brics_testimine\master.dwg";
                if (!File.Exists(sourceFileName))
                {
                    sourceFileName = @"C:\Users\Alex\Dropbox\DMT\Brics_testimine\master.dwg";
                }
            }

            sourceDb.ReadDwgFile(sourceFileName, System.IO.FileShare.Read, true, "");
            _Db.ObjectIdCollection blockIds = new _Db.ObjectIdCollection();


            using (_Db.Transaction sourceTrans = sourceDb.TransactionManager.StartTransaction())
            {
                _Db.BlockTable sourceBlockTable = sourceTrans.GetObject(sourceDb.BlockTableId, _Db.OpenMode.ForRead, false) as _Db.BlockTable;

                foreach (_Db.ObjectId sourceObject in sourceBlockTable)
                {
                    _Db.BlockTableRecord btr = sourceTrans.GetObject(sourceObject, _Db.OpenMode.ForRead, false) as _Db.BlockTableRecord;

                    if (!btr.IsAnonymous && !btr.IsLayout)
                    {
                        if (missingBlocks.Contains(btr.Name))
                        {
                            blockIds.Add(sourceObject);
                            missingBlocks.Remove(btr.Name);
                            write("[SETUP] " + btr.Name); // TODO REMOVE
                        }
                    }

                    btr.Dispose();
                }
            }

            if (missingBlocks.Count > 0)
            {
                return(false);
            }

            _Db.IdMapping mapping = new _Db.IdMapping();
            sourceDb.WblockCloneObjects(blockIds, _c.blockTable.Id, mapping, _Db.DuplicateRecordCloning.Replace, false);
            sourceDb.Dispose();

            write("[SETUP] Kõik puuduvad blockid on lisatud 'master' failist");
            return(true);
        }
Esempio n. 3
0
        /// <summary>
        /// Bulk-Befehl
        /// </summary>
        /// <param name="dwgFiles"></param>
        /// <returns></returns>
        private List <LayerInfo> GetLayerInfos(List <string> dwgFiles)
        {
            var layDict = new Dictionary <string, LayerInfo>();

            foreach (var fileName in dwgFiles)
            {
                int layerCount      = 0;
                int layerAddedCount = 0;
                var db = new _AcDb.Database(false, true);
                using (db)
                {
                    try
                    {
                        db.ReadDwgFile(fileName, System.IO.FileShare.Read, allowCPConversion: false, password: "");
                    }
                    catch (Exception ex)
                    {
                        log.WarnFormat("Fehler beim Öffnen der Zeichnung '{0}'!{1}", fileName, ex.Message);
                        continue;
                    }
                    using (_AcDb.Transaction trans = db.TransactionManager.StartTransaction())
                    {
                        try
                        {
                            _AcDb.LayerTable layTb = trans.GetObject(db.LayerTableId, _AcDb.OpenMode.ForRead) as _AcDb.LayerTable;
                            foreach (var ltrOid in layTb)
                            {
                                _AcDb.LayerTableRecord ltr = (_AcDb.LayerTableRecord)trans.GetObject(ltrOid, _AcDb.OpenMode.ForRead);
                                string layNameUC           = ltr.Name.ToUpperInvariant();
                                layerCount++;
                                if (!layDict.ContainsKey(layNameUC))
                                {
                                    layDict.Add(layNameUC, new LayerInfo(ltr, trans));
                                    layerAddedCount++;
                                }
                            }
                        }
                        finally
                        {
                            trans.Commit();
                        }
                    }
                }
                log.InfoFormat("{0}: {1} von {2} Layer hinzugefügt.", fileName, layerAddedCount, layerCount);
            }

            var linfos = layDict.Values.ToList();

            return(linfos);
        }
Esempio n. 4
0
        /// <summary>
        /// Öffnen einer Dwg ohne Editor
        /// </summary>
        //[_AcTrx.CommandMethod("Plan2TestSideDb")]
        static public void Plan2TestSideDb()
        {
            _AcAp.Document doc = _AcAp.Application.DocumentManager.MdiActiveDocument;
            _AcEd.Editor   ed  = doc.Editor;
            // Ask the user to select a file
            _AcEd.PromptResult res = ed.GetString("\nEnter the path of a DWG or DXF file: ");
            if (res.Status == _AcEd.PromptStatus.OK)
            {
                // Create a database and try to load the file
                _AcDb.Database db = new _AcDb.Database(false, true);
                using (db)
                {
                    try
                    {
                        db.ReadDwgFile(res.StringResult, System.IO.FileShare.Read, false, "");
                    }
                    catch (System.Exception)
                    {
                        ed.WriteMessage("\nUnable to read drawing file.");
                        return;
                    }

                    _AcDb.Transaction tr = db.TransactionManager.StartTransaction();
                    using (tr)
                    {
                        // Open the blocktable, get the modelspace
                        _AcDb.BlockTable       bt  = (_AcDb.BlockTable)tr.GetObject(db.BlockTableId, _AcDb.OpenMode.ForRead);
                        _AcDb.BlockTableRecord btr = (_AcDb.BlockTableRecord)tr.GetObject(bt[_AcDb.BlockTableRecord.ModelSpace], _AcDb.OpenMode.ForRead);

                        // Iterate through it, dumping objects
                        foreach (_AcDb.ObjectId objId in btr)
                        {
                            _AcDb.Entity ent = (_AcDb.Entity)tr.GetObject(objId, _AcDb.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());
                        }
                    }
                }
            }
        }
        public static bool ImportBlock(String fileBlock)
        {
            try
            {
                AcDb.Database refDb = new AcDb.Database(false, true);
                refDb.ReadDwgFile(fileBlock, System.IO.FileShare.Read, true, "");
                string        nameBlock = Path.GetFileNameWithoutExtension(fileBlock);
                AcDb.ObjectId idBTR     = db.Insert(AcDb.BlockTableRecord.ModelSpace, nameBlock, refDb, false);

                if (!idBTR.IsNull)
                {
                    return(true);
                }
            }
            catch { }

            return(false);
        }
Esempio n. 6
0
        public static AcDb.ObjectIdCollection ImportSymbolTableRecords <T>(
            /*this Database db,*/
            string sourceFile,
            params string[] recordNames)
            where T : AcDb.SymbolTable
        {
            using (AcDb.Database sourceDb = new AcDb.Database())
            {
                sourceDb.ReadDwgFile(sourceFile, System.IO.FileShare.Read, false, "");

                AcDb.ObjectId sourceTableId;
                AcDb.ObjectId targetTableId;

                switch (typeof(T).Name)
                {
                case "BlockTable":
                    sourceTableId = sourceDb.BlockTableId;
                    targetTableId = db.BlockTableId;
                    break;

                case "DimStyleTable":
                    sourceTableId = sourceDb.DimStyleTableId;
                    targetTableId = db.DimStyleTableId;
                    break;

                case "LayerTable":
                    sourceTableId = sourceDb.LayerTableId;
                    targetTableId = db.LayerTableId;
                    break;

                case "LinetypeTable":
                    sourceTableId = sourceDb.LinetypeTableId;
                    targetTableId = db.LinetypeTableId;
                    break;

                case "RegAppTable":
                    sourceTableId = sourceDb.RegAppTableId;
                    targetTableId = db.RegAppTableId;
                    break;

                case "TextStyleTable":
                    sourceTableId = sourceDb.TextStyleTableId;
                    targetTableId = db.TextStyleTableId;
                    break;

                case "UcsTable":
                    sourceTableId = sourceDb.UcsTableId;
                    targetTableId = db.UcsTableId;
                    break;

                case "ViewTable":
                    sourceTableId = sourceDb.ViewportTableId;
                    targetTableId = db.ViewportTableId;
                    break;

                case "ViewportTable":
                    sourceTableId = sourceDb.ViewportTableId;
                    targetTableId = db.ViewportTableId;
                    break;

                default:
                    throw new ArgumentException("\nImportSymbolTableRecords > Потрібен конкретний тип, похідний від SymbolTable");
                }

                using (AcDb.Transaction tr = sourceDb.TransactionManager.StartTransaction())
                {
                    T sourceTable = (T)tr.GetObject(sourceTableId, AcDb.OpenMode.ForRead);
                    AcDb.ObjectIdCollection idObjects = new AcDb.ObjectIdCollection();
                    foreach (string name in recordNames)
                    {
                        if (sourceTable.Has(name))
                        {
                            idObjects.Add(sourceTable[name]);
                        }
                    }
                    if (idObjects.Count == 0)
                    {
                        return(null);
                    }
                    AcDb.IdMapping idMap = new AcDb.IdMapping();
                    sourceDb.WblockCloneObjects(idObjects, targetTableId, idMap, AcDb.DuplicateRecordCloning.Replace, false);
                    tr.Commit();
                    AcDb.ObjectIdCollection retVal = new AcDb.ObjectIdCollection();
                    foreach (AcDb.ObjectId id in idObjects)
                    {
                        if (idMap[id].IsCloned)
                        {
                            retVal.Add(idMap[id].Value);
                        }
                    }
                    return(retVal.Count == 0 ? null : retVal);
                }
            }
        }
Esempio n. 7
0
        public static void ImportLayout(string layoutName, string filename)
        {
            // Get the current document and database
            _AcAp.Document acDoc   = _AcAp.Application.DocumentManager.MdiActiveDocument;
            _AcDb.Database acCurDb = acDoc.Database;

            // Specify the layout name and drawing file to work with
            //string layoutName = "MAIN AND SECOND FLOOR PLAN";
            //string filename = "C:\\AutoCAD\\Sample\\Sheet Sets\\Architectural\\A-01.dwg";

            // Create a new database object and open the drawing into memory
            _AcDb.Database acExDb = new _AcDb.Database(false, true);
            acExDb.ReadDwgFile(filename, Autodesk.AutoCAD.DatabaseServices.FileOpenMode.OpenForReadAndAllShare, true, "");

            // Create a transaction for the external drawing
            using (_AcDb.Transaction acTransEx = acExDb.TransactionManager.StartTransaction())
            {
                // Get the layouts dictionary
                _AcDb.DBDictionary layoutsEx = acTransEx.GetObject(acExDb.LayoutDictionaryId, _AcDb.OpenMode.ForRead) as _AcDb.DBDictionary;

                // Check to see if the layout exists in the external drawing
                if (layoutsEx.Contains(layoutName) == true)
                {
                    // Get the layout and block objects from the external drawing
                    _AcDb.Layout           layEx       = layoutsEx.GetAt(layoutName).GetObject(_AcDb.OpenMode.ForRead) as _AcDb.Layout;
                    _AcDb.BlockTableRecord blkBlkRecEx = acTransEx.GetObject(layEx.BlockTableRecordId, _AcDb.OpenMode.ForRead) as _AcDb.BlockTableRecord;

                    // Get the objects from the block associated with the layout
                    _AcDb.ObjectIdCollection idCol = new _AcDb.ObjectIdCollection();
                    foreach (_AcDb.ObjectId id in blkBlkRecEx)
                    {
                        idCol.Add(id);
                    }

                    // Create a transaction for the current drawing
                    using (_AcDb.Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
                    {
                        // Get the block table and create a new block
                        // then copy the objects between drawings
                        _AcDb.BlockTable blkTbl = acTrans.GetObject(acCurDb.BlockTableId, _AcDb.OpenMode.ForWrite) as _AcDb.BlockTable;
                        using (_AcDb.BlockTableRecord blkBlkRec = new _AcDb.BlockTableRecord())
                        {
                            int layoutCount = layoutsEx.Count - 1;

                            blkBlkRec.Name = "*Paper_Space" + layoutCount.ToString();
                            blkTbl.Add(blkBlkRec);
                            acTrans.AddNewlyCreatedDBObject(blkBlkRec, true);
                            acExDb.WblockCloneObjects(idCol, blkBlkRec.ObjectId, new _AcDb.IdMapping(), _AcDb.DuplicateRecordCloning.Ignore, false);

                            // Create a new layout and then copy properties between drawings
                            _AcDb.DBDictionary layouts = acTrans.GetObject(acCurDb.LayoutDictionaryId, _AcDb.OpenMode.ForWrite) as _AcDb.DBDictionary;
                            using (_AcDb.Layout lay = new _AcDb.Layout())
                            {
                                lay.LayoutName = layoutName;
                                lay.AddToLayoutDictionary(acCurDb, blkBlkRec.ObjectId);
                                acTrans.AddNewlyCreatedDBObject(lay, true);
                                lay.CopyFrom(layEx);

                                _AcDb.DBDictionary plSets = acTrans.GetObject(acCurDb.PlotSettingsDictionaryId, _AcDb.OpenMode.ForRead) as _AcDb.DBDictionary;

                                // Check to see if a named page setup was assigned to the layout,
                                // if so then copy the page setup settings
                                if (lay.PlotSettingsName != "")
                                {
                                    // Check to see if the page setup exists
                                    if (plSets.Contains(lay.PlotSettingsName) == false)
                                    {
                                        plSets.UpgradeOpen();

                                        using (_AcDb.PlotSettings plSet = new _AcDb.PlotSettings(lay.ModelType))
                                        {
                                            plSet.PlotSettingsName = lay.PlotSettingsName;
                                            plSet.AddToPlotSettingsDictionary(acCurDb);
                                            acTrans.AddNewlyCreatedDBObject(plSet, true);

                                            _AcDb.DBDictionary plSetsEx = acTransEx.GetObject(acExDb.PlotSettingsDictionaryId, _AcDb.OpenMode.ForRead) as _AcDb.DBDictionary;

                                            _AcDb.PlotSettings plSetEx = plSetsEx.GetAt(lay.PlotSettingsName).GetObject(_AcDb.OpenMode.ForRead) as _AcDb.PlotSettings;

                                            plSet.CopyFrom(plSetEx);
                                        }
                                    }
                                }
                            }
                        }

                        // Regen the drawing to get the layout tab to display
                        acDoc.Editor.Regen();

                        // Save the changes made
                        acTrans.Commit();
                    }
                }
                else
                {
                    // Display a message if the layout could not be found in the specified drawing
                    acDoc.Editor.WriteMessage("\nLayout '" + layoutName +
                                              "' could not be imported from '" + filename + "'.");
                }

                // Discard the changes made to the external drawing file
                acTransEx.Abort();
            }

            // Close the external drawing file
            acExDb.Dispose();
        }
Esempio n. 8
0
        private static void GetAllXRefFullPaths(string[] files, List <string> usedXRefFullPaths)
        {
            foreach (var fileName in files)
            {
                _AcDb.Database db = new _AcDb.Database(false, true);
                using (db)
                {
                    db.ReadDwgFile(fileName, System.IO.FileShare.Read, allowCPConversion: false, password: "");

                    using (_AcDb.Transaction tr = db.TransactionManager.StartTransaction())
                    {
                        _AcDb.BlockTable bt = (_AcDb.BlockTable)tr.GetObject(db.BlockTableId, _AcDb.OpenMode.ForRead);
                        foreach (var id in bt)
                        {
                            _AcDb.BlockTableRecord btr = (_AcDb.BlockTableRecord)tr.GetObject(id, _AcDb.OpenMode.ForRead);
                            if (btr.IsLayout)
                            {
                                foreach (var oid in btr)
                                {
                                    var ent = tr.GetObject(oid, _AcDb.OpenMode.ForRead);
                                    _AcDb.BlockReference bref = ent as _AcDb.BlockReference;
                                    if (bref != null)
                                    {
                                        if (bref.BlockTableRecord == default(_AcDb.ObjectId))
                                        {
                                            try
                                            {
                                                log.WarnFormat(CultureInfo.CurrentCulture, "Block in '{0}' hat keinen Blocktable-Record. Handle = {1}", bref.BlockName, bref.Handle);
                                            }
                                            catch (Exception ex)
                                            {
                                                log.Error(ex.Message, ex);
                                            }
                                        }
                                        else
                                        {
                                            var bd = (_AcDb.BlockTableRecord)tr.GetObject(bref.BlockTableRecord, _AcDb.OpenMode.ForRead);
                                            if (bd.IsFromExternalReference)
                                            {
                                                string xpath        = bd.PathName;
                                                string completePath = string.Empty;
                                                if (!System.IO.Path.IsPathRooted(xpath))
                                                {
                                                    // relativer pfad
                                                    string cpath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(fileName), xpath);
                                                    completePath = System.IO.Path.GetFullPath(cpath);
                                                }
                                                else
                                                {
                                                    // absoluter pfad
                                                    completePath = System.IO.Path.GetFullPath(xpath);
                                                }
                                                completePath = completePath.ToUpperInvariant();
                                                if (!usedXRefFullPaths.Contains(completePath))
                                                {
                                                    usedXRefFullPaths.Add(completePath);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        tr.Commit();
                    }
                }
            }
        }