Пример #1
0
        public static void DetachRasterImage(LandRasterImage landRastr)
        {
            AcDb.Database curDb = AcApp.DocumentManager.MdiActiveDocument.Database;
            using (AcDb.Transaction tr = curDb.TransactionManager.StartTransaction())
            {
                AcDb.RasterImageDef rasterDef;
                //bool bRasterDefCreated = false;
                AcDb.ObjectId imgDefId;

                AcDb.ObjectId imgDctID = AcDb.RasterImageDef.GetImageDictionary(curDb);
                if (imgDctID.IsNull)
                {
                    imgDctID = AcDb.RasterImageDef.CreateImageDictionary(curDb);
                }

                AcDb.DBDictionary imgDict = tr.GetObject(imgDctID, AcDb.OpenMode.ForWrite) as AcDb.DBDictionary;

                if (imgDict.Contains(landRastr.ImageName))
                {
                    imgDefId  = imgDict.GetAt(landRastr.ImageName);
                    rasterDef = tr.GetObject(imgDefId, AcDb.OpenMode.ForWrite) as AcDb.RasterImageDef;
                    if (rasterDef.IsLoaded)
                    {
                        rasterDef.Unload(true);
                        imgDict.Remove(landRastr.ImageName);
                    }
                }
                tr.Commit();
            }
        }
Пример #2
0
        private void insertReinforcmentMark(string mark, G.Point IP)
        {
            string layerName = "K023TL";
            string styleName = "dmt_M" + (int)Math.Round(L._V_.Z_DRAWING_SCALE);

            textStyleHandler();
            blockHandler();
            leaderStyleHandler(styleName, (int)Math.Round(L._V_.Z_DRAWING_SCALE));

            _Db.DBDictionary mleaderStyleTable = _c.trans.GetObject(_c.db.MLeaderStyleDictionaryId, _Db.OpenMode.ForWrite) as _Db.DBDictionary;

            _Ge.Point3d insertPointLeader = new _Ge.Point3d(IP.X, IP.Y, 0);
            _Ge.Point3d insertPointText   = new _Ge.Point3d(IP.X + 7.5 * L._V_.Z_DRAWING_SCALE, IP.Y + 7.5 * L._V_.Z_DRAWING_SCALE, 0);

            _Db.MText mText = new _Db.MText();
            mText.SetDatabaseDefaults();
            mText.Contents = mark;

            _Db.MLeader leader = new _Db.MLeader();
            leader.SetDatabaseDefaults();
            leader.ContentType = _Db.ContentType.MTextContent;
            leader.MText       = mText;
            leader.AddLeaderLine(insertPointLeader);
            leader.TextLocation = insertPointText;
            leader.MLeaderStyle = mleaderStyleTable.GetAt(styleName);

            leader.Layer = layerName;

            _c.modelSpace.AppendEntity(leader);
            _c.trans.AddNewlyCreatedDBObject(leader, true);
        }
Пример #3
0
        public _AcDb.ResultBuffer LayoutListSelected(_AcDb.ResultBuffer args)
        {
            if (args != null)
            {
                throw new TooFewArgsException();
            }

            _AcAp.Document doc = _AcAp.Application.DocumentManager.MdiActiveDocument;
            _AcDb.Database db  = doc.Database;
            //Editor ed = doc.Editor;
            //LayoutManager layoutMgr = LayoutManager.Current;
            List <string> layouts = new List <string>();

            _AcDb.ResultBuffer res = new _AcDb.ResultBuffer();

            using (_AcDb.Transaction tr = db.TransactionManager.StartTransaction())
            {
                _AcDb.DBDictionary layoutDic =
                    (_AcDb.DBDictionary)tr.GetObject(db.LayoutDictionaryId, _AcDb.OpenMode.ForRead, openErased: false);

                foreach (_AcDb.DBDictionaryEntry entry in layoutDic)
                {
                    _AcDb.Layout layout =
                        (_AcDb.Layout)tr.GetObject(entry.Value, _AcDb.OpenMode.ForRead);

                    string layoutName = layout.LayoutName;

                    if (layout.TabSelected)
                    {
                        layouts.Add(layoutName);
                    }
                }

                tr.Commit();
            }

            layouts.Remove("Model");

            if (0 < layouts.Count)
            {
                layouts.Sort();

                foreach (string layoutName in layouts)
                {
                    res.Add(new _AcDb.TypedValue((int)(_AcBrx.LispDataType.Text), layoutName));
                }

                return(res);
            }

            else
            {
                return(null);
            }
        }
Пример #4
0
        private void leaderStyleHandler(string styleName, int scale)
        {
            _Db.DBDictionary   mleaderStyleTable = _c.trans.GetObject(_c.db.MLeaderStyleDictionaryId, _Db.OpenMode.ForWrite) as _Db.DBDictionary;
            _Db.TextStyleTable txtStyleTable     = _c.trans.GetObject(_c.db.TextStyleTableId, _Db.OpenMode.ForWrite) as _Db.TextStyleTable;

            if (!mleaderStyleTable.Contains(styleName))
            {
                _Db.MLeaderStyle newStyle = new _Db.MLeaderStyle();

                newStyle.Annotative    = _Db.AnnotativeStates.False;
                newStyle.ArrowSize     = 3.0;
                newStyle.ArrowSymbolId = _c.blockTable["_NONE"];
                //newStyle.BlockColor=; //BYBLOCK
                newStyle.BlockConnectionType         = _Db.BlockConnectionType.ConnectExtents;
                newStyle.BlockId                     = _Db.ObjectId.Null;
                newStyle.BlockRotation               = 0;
                newStyle.BlockScale                  = new _Ge.Scale3d(1, 1, 1);
                newStyle.BreakSize                   = 0;
                newStyle.ContentType                 = _Db.ContentType.MTextContent;
                newStyle.DefaultMText                = new _Db.MText();
                newStyle.DoglegLength                = 8;
                newStyle.DrawLeaderOrderType         = _Db.DrawLeaderOrderType.DrawLeaderHeadFirst;
                newStyle.DrawMLeaderOrderType        = _Db.DrawMLeaderOrderType.DrawLeaderFirst;
                newStyle.EnableBlockRotation         = true;
                newStyle.EnableBlockScale            = true;
                newStyle.EnableDogleg                = false;
                newStyle.EnableFrameText             = false;
                newStyle.EnableLanding               = true;
                newStyle.FirstSegmentAngleConstraint = _Db.AngleConstraint.DegreesAny;
                newStyle.LandingGap                  = 1;
                newStyle.LeaderLineColor             = _Cm.Color.FromColorIndex(_Cm.ColorMethod.None, 142);
                newStyle.LeaderLineType              = _Db.LeaderType.StraightLeader;
                //newStyle.LeaderLineTypeId=; //BYBLOCK
                //newStyle.LeaderLineWeight=; //BYBLOCK
                newStyle.MaxLeaderSegmentsPoints = 2;
                newStyle.Scale = scale;
                newStyle.SecondSegmentAngleConstraint = _Db.AngleConstraint.DegreesAny;
                newStyle.TextAlignAlwaysLeft          = false;
                newStyle.TextAlignmentType            = _Db.TextAlignmentType.LeftAlignment;
                newStyle.TextAngleType = _Db.TextAngleType.HorizontalAngle;
                //newStyle.TextColor=; //BYBLOCK
                newStyle.TextHeight  = 2.5;
                newStyle.TextStyleId = txtStyleTable["Stommest"];

                newStyle.SetTextAttachmentType(_Db.TextAttachmentType.AttachmentBottomOfTopLine, _Db.LeaderDirectionType.LeftLeader);  // Left attachment
                newStyle.SetTextAttachmentType(_Db.TextAttachmentType.AttachmentBottomOfTopLine, _Db.LeaderDirectionType.RightLeader); // Right attachment

                newStyle.PostMLeaderStyleToDb(_c.db, styleName);
                _c.trans.AddNewlyCreatedDBObject(newStyle, true);
                write("[OUTPUT] MLeader style '" + styleName + "' created");
            }
        }
Пример #5
0
        //Plan2Ext.Layouts.ImportLayout("02.OG B-Bau", @"D:\Plan2\Data\Plan2RenameBlocks\work\02_OGa.dwg");
        //Plan2Ext.Layouts.CreateLayout("alx");
        //var layoutNames = Plan2Ext.Layouts.GetLayoutNames();

        public static List <string> GetLayoutNames()
        {
            var layoutNames = new List <string>();

            _AcAp.Document acDoc   = _AcAp.Application.DocumentManager.MdiActiveDocument;
            _AcDb.Database acCurDb = acDoc.Database;

            using (_AcDb.Transaction trans = acCurDb.TransactionManager.StartTransaction())
            {
                _AcDb.DBDictionary lays = trans.GetObject(acCurDb.LayoutDictionaryId, _AcDb.OpenMode.ForRead) as _AcDb.DBDictionary;

                foreach (_AcDb.DBDictionaryEntry item in lays)
                {
                    layoutNames.Add(item.Key);
                }
                trans.Commit();
            }
            return(layoutNames);
        }
Пример #6
0
        public void renameLayout(string find, string replace)
        {
            List <string> layouts = new List <string>();

            _Db.DBDictionary lays = _c.trans.GetObject(_c.db.LayoutDictionaryId, _Db.OpenMode.ForWrite) as _Db.DBDictionary;

            foreach (_Db.DBDictionaryEntry item in lays)
            {
                if (item.Key.Contains(find))
                {
                    string name = item.Key;
                    layouts.Add(name);
                }
            }

            if (layouts.Count > 0)
            {
                _Db.LayoutManager lm = _Db.LayoutManager.Current;

                foreach (string lay in layouts)
                {
                    string newname = lay.Replace(find, replace);
                    lm.RenameLayout(lay, newname);
                }


                string       randomName = generateRandomString(40);
                _Db.ObjectId id         = lm.GetLayoutId(randomName);

                if (!id.IsValid)
                {
                    id = lm.CreateLayout(randomName);
                }

                lm.DeleteLayout(randomName);

                lm.Dispose();
            }
        }
Пример #7
0
        //[_AcTrx.CommandMethod("ListLayouts")]
        public static void ListLayoutsMethod()
        {
            _AcAp.Document doc

                = _AcAp.Application.DocumentManager.MdiActiveDocument;

            _AcDb.Database db = doc.Database;

            _AcEd.Editor ed = doc.Editor;



            _AcDb.LayoutManager layoutMgr = _AcDb.LayoutManager.Current;

            ed.WriteMessage

            (

                String.Format

                (

                    "{0}Active Layout is : {1}",

                    Environment.NewLine,

                    layoutMgr.CurrentLayout

                )

            );



            ed.WriteMessage

            (

                String.Format

                (

                    "{0}Number of Layouts: {1}{0}List of all Layouts:",

                    Environment.NewLine,

                    layoutMgr.LayoutCount

                )

            );



            using (_AcDb.Transaction tr

                       = db.TransactionManager.StartTransaction())
            {
                _AcDb.DBDictionary layoutDic

                    = tr.GetObject(

                          db.LayoutDictionaryId,

                          _AcDb.OpenMode.ForRead,

                          false

                          ) as _AcDb.DBDictionary;



                foreach (_AcDb.DBDictionaryEntry entry in layoutDic)
                {
                    _AcDb.ObjectId layoutId = entry.Value;



                    _AcDb.Layout layout

                        = tr.GetObject(

                              layoutId,

                              _AcDb.OpenMode.ForRead

                              ) as _AcDb.Layout;



                    ed.WriteMessage(

                        String.Format(

                            "{0}--> {1}",

                            Environment.NewLine,

                            layout.LayoutName

                            )

                        );
                }

                tr.Commit();
            }
        }
Пример #8
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();
        }
Пример #9
0
        // Just for Testpurpose
        private static void GetAssignmentsDict()
        {
            var doc = _AcAp.Application.DocumentManager.MdiActiveDocument;
            var db  = doc.Database;

            try
            {
                using (_AcDb.Transaction trans =

                           db.TransactionManager.StartTransaction())
                {
                    // Find the NOD in the database

                    _AcDb.DBDictionary nod = (_AcDb.DBDictionary)trans.GetObject(

                        db.NamedObjectsDictionaryId, _AcDb.OpenMode.ForWrite);



                    //// We use Xrecord class to store data in Dictionaries

                    //Xrecord myXrecord = new Xrecord();

                    //myXrecord.Data = new _AcDb.ResultBuffer(

                    //        new _AcDb.TypedValue((int)DxfCode.Int16, 1234),

                    //        new _AcDb.TypedValue((int)DxfCode.Text,

                    //                        "This drawing has been processed"));



                    //// Create the entry in the Named Object Dictionary

                    //nod.SetAt("MyData", myXrecord);

                    //trans.AddNewlyCreatedDBObject(myXrecord, true);



                    // Now let's read the data back and print them out

                    //  to the Visual Studio's Output window

                    _AcDb.ObjectId myDataId = nod.GetAt("XRECLIST");

                    var readBack = trans.GetObject(

                        myDataId, _AcDb.OpenMode.ForRead);

                    var readBack2 = readBack as _AcDb.Xrecord;
                    if (readBack2 != null)
                    {
                        foreach (_AcDb.TypedValue value in readBack2.Data)
                        {
                            System.Diagnostics.Debug.Print(

                                "===== OUR DATA: " + value.TypeCode.ToString()

                                + ". " + value.Value.ToString());
                        }



                        trans.Commit();
                    }
                } // using



                db.SaveAs(@"C:\Temp\Test.dwg", _AcDb.DwgVersion.Current);
            }

            catch (System.Exception e)
            {
                System.Diagnostics.Debug.Print(e.ToString());
            }
        }
Пример #10
0
        public static void AttachRasterImage(LandRasterImage landRastr)
        {
            AcEd.Editor ed = AcApp.DocumentManager.MdiActiveDocument.Editor;

            //bool bRasterDefCreated = false;

            AcDb.Database curDb = AcApp.DocumentManager.MdiActiveDocument.Database;

            using (AcDb.Transaction tr = curDb.TransactionManager.StartTransaction())
            {
                AcDb.RasterImageDef rasterDef;
                AcDb.ObjectId       imgDefId;
                AcDb.ObjectId       imgDctID = AcDb.RasterImageDef.GetImageDictionary(curDb);
                if (imgDctID.IsNull)
                {
                    imgDctID = AcDb.RasterImageDef.CreateImageDictionary(curDb);
                }

                AcDb.DBDictionary imgDict = (AcDb.DBDictionary)tr.GetObject(imgDctID, AcDb.OpenMode.ForRead);

                if (imgDict.Contains(landRastr.ImageName))
                {
                    imgDefId  = imgDict.GetAt(landRastr.ImageName);
                    rasterDef = (AcDb.RasterImageDef)tr.GetObject(imgDefId, AcDb.OpenMode.ForWrite);
                    if (rasterDef.IsLoaded)
                    {
                        //return;
                    }
                }
                else
                {
                    AcPApp.AcadApplication app    = AcApp.AcadApplication as AcPApp.AcadApplication;
                    AcPApp.AcadDocument    doc    = app.ActiveDocument;
                    AcPDb.AcadModelSpace   mSpace = doc.ModelSpace;
                    AcPDb.AcadRasterImage  ri     = mSpace.AddRaster(landRastr.FileName, landRastr.InsertPoint.ToArray(), 1000, 0);
                    ri.Name         = landRastr.ImageName;
                    ri.Transparency = true;
                    ServiceCAD.CreateLayer(landRastr.Layer);
                    ri.Layer       = landRastr.Layer;
                    ri.ImageHeight = 1000;
                    ri.ImageWidth  = 1000;

                    tr.Commit();
                    return;
                }

                AcDb.BlockTable blkTbl =
                    (AcDb.BlockTable)tr.GetObject(curDb.BlockTableId, AcDb.OpenMode.ForRead);

                AcDb.BlockTableRecord acBlkTblRec =
                    (AcDb.BlockTableRecord)tr.GetObject(blkTbl[AcDb.BlockTableRecord.ModelSpace], AcDb.OpenMode.ForWrite);

                using (AcDb.RasterImage rasterImage = new AcDb.RasterImage())
                {
                    rasterImage.ImageDefId = imgDefId;

                    AcGe.Vector3d width;
                    AcGe.Vector3d height;

                    AcGe.Matrix3d ucs  = ed.CurrentUserCoordinateSystem;
                    double        size = 1000;

                    width  = new AcGe.Vector3d(size, 0, 0);
                    height = new AcGe.Vector3d(0, size, 0);


                    AcGe.Point3d            insPt            = landRastr.InsertPoint;
                    AcGe.CoordinateSystem3d coordinateSystem = new AcGe.CoordinateSystem3d(insPt.TransformBy(ucs), width.TransformBy(ucs), height.TransformBy(ucs));
                    rasterImage.Orientation = coordinateSystem;
                    rasterImage.Rotation    = 0;
                    rasterImage.ShowImage   = true;

                    acBlkTblRec.AppendEntity(rasterImage);
                    tr.AddNewlyCreatedDBObject(rasterImage, true);

                    rasterImage.AssociateRasterDef(rasterDef);
                }
                tr.Commit();
            }
        }
Пример #11
0
        private void ReplaceLayerInEntities(Dictionary <string, string> substDict, _AcDb.Transaction trans, _AcDb.Database db)
        {
            //string oldLayer = linfo.OldLayer;
            //string newLayer = linfo.NewLayer;

            _AcDb.BlockTable blkTable = (_AcDb.BlockTable)trans.GetObject(db.BlockTableId, _AcDb.OpenMode.ForRead);
            foreach (var id in blkTable)
            {
                _AcDb.BlockTableRecord btRecord = (_AcDb.BlockTableRecord)trans.GetObject(id, _AcDb.OpenMode.ForRead);
                //System.Diagnostics.Debug.WriteLine(string.Format(CultureInfo.CurrentCulture, "Block: {0}", btRecord.Name));

                //btRecord.UpgradeOpen();

                foreach (var entId in btRecord)
                {
                    _AcDb.Entity entity = (_AcDb.Entity)trans.GetObject(entId, _AcDb.OpenMode.ForWrite);

                    CheckSetLayer(substDict, entity);
                    //if (string.Compare(entity.Layer, oldLayer, StringComparison.OrdinalIgnoreCase) == 0)
                    //{
                    //    entity.Layer = newLayer;
                    //}

                    _AcDb.BlockReference block = entity as _AcDb.BlockReference;
                    if (block != null)
                    {
                        // sequend correction
                        string saveLay = block.Layer;
                        block.Layer = "0";
                        block.Layer = saveLay;

                        foreach (var att in block.AttributeCollection)
                        {
                            _AcDb.ObjectId           attOid = (_AcDb.ObjectId)att;
                            _AcDb.AttributeReference attrib = trans.GetObject(attOid, _AcDb.OpenMode.ForWrite) as _AcDb.AttributeReference;
                            if (attrib != null)
                            {
                                CheckSetLayer(substDict, attrib);
                                //if (string.Compare(attrib.Layer, oldLayer, StringComparison.OrdinalIgnoreCase) == 0)
                                //{
                                //    attrib.Layer = newLayer;
                                //}
                            }
                        }
                    }
                }

                //}
            }

            // Layouts iterieren
            _AcDb.DBDictionary layoutDict = (_AcDb.DBDictionary)trans.GetObject(db.LayoutDictionaryId, _AcDb.OpenMode.ForRead);
            foreach (var loEntry in layoutDict)
            {
                if (loEntry.Key.ToUpperInvariant() == "MODEL")
                {
                    continue;
                }
                _AcDb.Layout lo = (_AcDb.Layout)trans.GetObject(loEntry.Value, _AcDb.OpenMode.ForRead, false);
                if (lo == null)
                {
                    continue;
                }
                //System.Diagnostics.Debug.WriteLine(string.Format(CultureInfo.CurrentCulture, "Layout: {0}", lo.LayoutName));

                _AcDb.BlockTableRecord btRecord = (_AcDb.BlockTableRecord)trans.GetObject(lo.BlockTableRecordId, _AcDb.OpenMode.ForRead);
                foreach (var entId in btRecord)
                {
                    _AcDb.Entity entity = (_AcDb.Entity)trans.GetObject(entId, _AcDb.OpenMode.ForWrite);

                    CheckSetLayer(substDict, entity);

                    //if (string.Compare(entity.Layer, oldLayer, StringComparison.OrdinalIgnoreCase) == 0)
                    //{
                    //    entity.Layer = newLayer;
                    //}

                    _AcDb.BlockReference block = entity as _AcDb.BlockReference;
                    if (block != null)
                    {
                        string saveLay = block.Layer;
                        block.Layer = "0";
                        block.Layer = saveLay;

                        foreach (var att in block.AttributeCollection)
                        {
                            _AcDb.ObjectId           attOid = (_AcDb.ObjectId)att;
                            _AcDb.AttributeReference attrib = trans.GetObject(attOid, _AcDb.OpenMode.ForWrite) as _AcDb.AttributeReference;
                            if (attrib != null)
                            {
                                CheckSetLayer(substDict, attrib);
                                //if (string.Compare(attrib.Layer, oldLayer, StringComparison.OrdinalIgnoreCase) == 0)
                                //{

                                //    //attrib.UpgradeOpen();
                                //    attrib.Layer = newLayer;
                                //}
                            }
                        }
                    }
                }
            }
        }
Пример #12
0
        private void SubVarianLoadRaster()
        {
            String PIC_NAME = "C:\\_Temp_\\5525.TIF";

            AcDb.Database CurDb = AcAp.Application.DocumentManager.MdiActiveDocument.Database;


            using (AcDb.Transaction acTrans = CurDb.TransactionManager.StartTransaction())
            {
                try
                {
                    AcDb.ObjectId dictId = AcDb.RasterImageDef.GetImageDictionary(CurDb);
                    if (dictId == null)
                    {
                        dictId = AcDb.RasterImageDef.CreateImageDictionary(CurDb);
                    }

                    AcDb.DBDictionary   dict    = acTrans.GetObject(dictId, AcDb.OpenMode.ForRead) as AcDb.DBDictionary;
                    String              recName = "5525";
                    AcDb.RasterImageDef rid     = new AcDb.RasterImageDef
                    {
                        SourceFileName = PIC_NAME
                    };
                    dict.UpgradeOpen();
                    AcDb.ObjectId defId = dict.SetAt(recName, rid);

                    rid.Load();

                    acTrans.AddNewlyCreatedDBObject(rid, true);
                    AcDb.RasterImage ri = new AcDb.RasterImage
                    {
                        ImageDefId  = defId,
                        ShowImage   = true,
                        Orientation = new AcGe.CoordinateSystem3d(new AcGe.Point3d(200, 300, 0), new AcGe.Vector3d(1, 0, 0), new AcGe.Vector3d(0, 1, 0))
                    };
                    AcDb.BlockTable       bt  = acTrans.GetObject(CurDb.BlockTableId, AcDb.OpenMode.ForRead, false) as AcDb.BlockTable;
                    AcDb.BlockTableRecord btr = acTrans.GetObject(bt[AcDb.BlockTableRecord.ModelSpace], AcDb.OpenMode.ForWrite, false) as AcDb.BlockTableRecord;
                    btr.AppendEntity(ri);
                    acTrans.AddNewlyCreatedDBObject(ri, true);
                    ri.AssociateRasterDef(rid);
                    ri.TransformBy(AcGe.Matrix3d.Scaling(1000, new AcGe.Point3d(200, 300, 0)));
                }
                catch
                {
                    return;
                }
            }

            /*
             *     Try
             *              Dim PIC_NAME As String = original_picture_name
             *              Dim dictId As ObjectId = RasterImageDef.GetImageDictionary(Db)
             *              If dictId = Nothing Then
             *                  dictId = RasterImageDef.CreateImageDictionary(Db)
             *              End If
             *              Dim dict As DBDictionary = CType(acTrans.GetObject(dictId, OpenMode.ForRead), DBDictionary)
             *              Dim recName As String = Picture_name_in_drawing
             *              Dim rid As RasterImageDef = New RasterImageDef
             *              rid.SourceFileName = PIC_NAME
             *              dict.UpgradeOpen()
             *              Dim defId As ObjectId = dict.SetAt(recName, rid)
             *              rid.Load()
             *              acTrans.AddNewlyCreatedDBObject(rid, True)
             *              Dim ri As RasterImage = New RasterImage
             *              ri.ImageDefId = defId
             *              ri.ShowImage = True
             *              ri.Orientation = New CoordinateSystem3d(New Point3d(x1, y1, 0), New Vector3d(1, 0, 0), New Vector3d(0, 1, 0))
             *              Dim bt As BlockTable
             *              Dim btr As BlockTableRecord
             *              bt = acTrans.GetObject(Db.BlockTableId, OpenMode.ForRead, False)
             *              btr = acTrans.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite, False)
             *              btr.AppendEntity(ri)
             *              acTrans.AddNewlyCreatedDBObject(ri, True)
             *              ri.AssociateRasterDef(rid)
             *              ri.TransformBy(Matrix3d.Scaling(picture_scale, New Point3d(x1, y1, 0)))
             *       Catch
             *
             *              Exit Sub
             *       End Try
             *
             */
        }
Пример #13
0
        public void AttachRasterImage()
        {
            // Get the current database and start a transaction
            AcDb.Database acCurDb;
            acCurDb = AcAp.Application.DocumentManager.MdiActiveDocument.Database;

            using (AcDb.Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                // Define the name and image to use
                string strImgName  = "5525";
                string strFileName = "C:\\_Temp_\\5525.TIF";

                AcDb.RasterImageDef acRasterDef;
                bool          bRasterDefCreated = false;
                AcDb.ObjectId acImgDefId;

                // Get the image dictionary
                AcDb.ObjectId acImgDctID = AcDb.RasterImageDef.GetImageDictionary(acCurDb);

                // Check to see if the dictionary does not exist, it not then create it
                if (acImgDctID.IsNull)
                {
                    acImgDctID = AcDb.RasterImageDef.CreateImageDictionary(acCurDb);
                }

                // Open the image dictionary
                AcDb.DBDictionary acImgDict = acTrans.GetObject(acImgDctID, AcDb.OpenMode.ForRead) as AcDb.DBDictionary;

                // Check to see if the image definition already exists
                if (acImgDict.Contains(strImgName))
                {
                    acImgDefId = acImgDict.GetAt(strImgName);

                    acRasterDef = acTrans.GetObject(acImgDefId, AcDb.OpenMode.ForWrite) as AcDb.RasterImageDef;
                }
                else
                {
                    // Create a raster image definition
                    AcDb.RasterImageDef acRasterDefNew = new AcDb.RasterImageDef
                    {
                        // Set the source for the image file
                        SourceFileName = strFileName
                    };

                    acImgDict.UpgradeOpen();
                    acImgDefId = acImgDict.SetAt(strImgName, acRasterDefNew);

                    // Load the image into memory
                    acRasterDefNew.Load();


                    // Add the image definition to the dictionary
                    ////acImgDict.UpgradeOpen();
                    ////acImgDefId = acImgDict.SetAt(strImgName, acRasterDefNew);

                    acTrans.AddNewlyCreatedDBObject(acRasterDefNew, true);

                    /***/

                    acRasterDef = acRasterDefNew;

                    bRasterDefCreated = true;
                }

                // Open the Block table for read
                AcDb.BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, AcDb.OpenMode.ForRead) as AcDb.BlockTable;

                // Open the Block table record Model space for write
                AcDb.BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acBlkTbl[AcDb.BlockTableRecord.ModelSpace],
                                                AcDb.OpenMode.ForWrite) as AcDb.BlockTableRecord;


                // Create the new image and assign it the image definition
                using (AcDb.RasterImage acRaster = new AcDb.RasterImage())
                {
                    acRaster.ImageDefId = acImgDefId;

                    // Use ImageWidth and ImageHeight to get the size of the image in pixels (1024 x 768).
                    // Use ResolutionMMPerPixel to determine the number of millimeters in a pixel so you
                    // can convert the size of the drawing into other units or millimeters based on the
                    // drawing units used in the current drawing.

                    // Define the width and height of the image
                    AcGe.Vector3d width;
                    AcGe.Vector3d height;

                    // Check to see if the measurement is set to English (Imperial) or Metric units
                    if (acCurDb.Measurement == AcDb.MeasurementValue.English)
                    {
                        width  = new AcGe.Vector3d((acRasterDef.ResolutionMMPerPixel.X * acRaster.ImageWidth) / 25.4, 0, 0);
                        height = new AcGe.Vector3d(0, (acRasterDef.ResolutionMMPerPixel.Y * acRaster.ImageHeight) / 25.4, 0);
                    }
                    else
                    {
                        width  = new AcGe.Vector3d(acRasterDef.ResolutionMMPerPixel.X * acRaster.ImageWidth, 0, 0);
                        height = new AcGe.Vector3d(0, acRasterDef.ResolutionMMPerPixel.Y * acRaster.ImageHeight, 0);
                    }

                    // Define the position for the image
                    AcGe.Point3d insPt = new AcGe.Point3d(52000.0, 56000.0, 100.0);

                    // Define and assign a coordinate system for the image's orientation
                    AcGe.CoordinateSystem3d coordinateSystem = new AcGe.CoordinateSystem3d(insPt, width * 2, height * 2);
                    acRaster.Orientation = coordinateSystem;

                    // Set the rotation angle for the image
                    acRaster.Rotation = 0;

                    // Add the new object to the block table record and the transaction
                    acBlkTblRec.AppendEntity(acRaster);
                    acTrans.AddNewlyCreatedDBObject(acRaster, true);

                    // Connect the raster definition and image together so the definition
                    // does not appear as "unreferenced" in the External References palette.
                    /*AcDb.RasterImage.EnableReactors(true);*/
                    acRaster.AssociateRasterDef(acRasterDef);
                    acRaster.ShowImage         = true;
                    acRaster.ColorIndex        = 200;
                    acRaster.ImageTransparency = true;
                    acRaster.Orientation       = new AcGe.CoordinateSystem3d(insPt, new AcGe.Vector3d(1000, 0, 0), new AcGe.Vector3d(0, 2000, 0));

                    if (bRasterDefCreated)
                    {
                        acRasterDef.Dispose();
                    }
                }

                // Save the new object to the database
                acTrans.Commit();

                // Dispose of the transaction
            }
        }
Пример #14
0
        internal List <ZuweisungsInfo> GetAssignmentsDict()
        {
            List <ZuweisungsInfo> zuweisungen = new List <ZuweisungsInfo>();

            var doc = _AcAp.Application.DocumentManager.MdiActiveDocument;

            if (doc == null)
            {
                return(zuweisungen);
            }

            var db = doc.Database;

            using (_AcDb.Transaction trans = db.TransactionManager.StartTransaction())
            {
                try
                {
                    // Find the NOD in the database

                    _AcDb.ObjectId myDataId = default(_AcDb.ObjectId);
                    using (_AcDb.DBDictionary nod = (_AcDb.DBDictionary)trans.GetObject(
                               db.NamedObjectsDictionaryId, _AcDb.OpenMode.ForRead))
                    {
                        foreach (var subnode in nod)
                        {
                            if (subnode.Key == "AutoIdAssignments")
                            {
                                myDataId = subnode.Value;
                                break;
                            }
                        }
                    }

                    if (myDataId != default(_AcDb.ObjectId))
                    {
                        //ObjectId myDataId = nod.GetAt("AutoIdAssignments");
                        var readBack  = trans.GetObject(myDataId, _AcDb.OpenMode.ForRead);
                        var readBack2 = readBack as _AcDb.Xrecord;
                        if (readBack2 != null)
                        {
                            List <string> rbs = new List <string>();
                            foreach (_AcDb.TypedValue value in readBack2.Data)
                            {
                                rbs.Add(value.Value.ToString());
                            }

                            for (int i = 0; i < rbs.Count; i += 2)
                            {
                                zuweisungen.Add(new ZuweisungsInfo()
                                {
                                    FromAtt = rbs[i], ToAtt = rbs[i + 1]
                                });
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    // todo: log error
                }
                finally
                {
                    trans.Commit();
                }
            }

            if (zuweisungen.Count > 0)
            {
                // zuweisungen erhalten, wenn es noch keine gibt
                _Zuweisungen.Clear();
                _Zuweisungen.AddRange(zuweisungen);
            }

            return(_Zuweisungen);
        }
Пример #15
0
        //[_AcTrx.CommandMethod("SaveView")]
        //public static void SaveView()
        //{
        //    Globs.SaveView("Test");
        //}
        //[_AcTrx.CommandMethod("RestoreView")]
        //public static void RestoreView()
        //{
        //    Globs.RestoreView("Test");
        //}

        //[_AcTrx.CommandMethod("TestMakeLayer")]
        public void TestMakeLayer()
        {
            var doc      = _AcAp.Application.DocumentManager.MdiActiveDocument;
            var db       = doc.Database;
            var layerOid = CreateNewLayer(doc, db);

            using (_AcDb.Transaction trans = doc.TransactionManager.StartTransaction())
            {
                _AcDb.BlockTable blkTable = (_AcDb.BlockTable)trans.GetObject(db.BlockTableId, _AcDb.OpenMode.ForRead);
                foreach (var id in blkTable)
                {
                    _AcDb.BlockTableRecord btRecord = (_AcDb.BlockTableRecord)trans.GetObject(id, _AcDb.OpenMode.ForRead);
                    //if (!btRecord.IsLayout)
                    //{
                    //Access to the block (not model/paper space)
                    System.Diagnostics.Debug.WriteLine(string.Format(CultureInfo.CurrentCulture, "Block: {0}", btRecord.Name));
                    //MgdAcApplication.DocumentManager.MdiActiveDocument.Editor.WriteMessage(string.Format("\nBlock name: {0}", btRecord.Name));

                    btRecord.UpgradeOpen();

                    foreach (var entId in btRecord)
                    {
                        _AcDb.Entity entity = (_AcDb.Entity)trans.GetObject(entId, _AcDb.OpenMode.ForRead);

                        entity.UpgradeOpen();

                        //Access to the entity
                        //MgdAcApplication.DocumentManager.MdiActiveDocument.Editor.WriteMessage(string.Format("\nHandle: {0}", entity.Handle));
                        //System.Diagnostics.Debug.WriteLine(string.Format(CultureInfo.CurrentCulture, "Handle: {0}, Layer: {1}", entity.Handle, entity.Layer));
                        entity.Layer = "MyTest";

                        _AcDb.BlockReference block = entity as _AcDb.BlockReference;
                        if (block != null)
                        {
                            foreach (var att in block.AttributeCollection)
                            {
                                _AcDb.ObjectId           attOid = (_AcDb.ObjectId)att;
                                _AcDb.AttributeReference attrib = trans.GetObject(attOid, _AcDb.OpenMode.ForWrite) as _AcDb.AttributeReference;
                                if (attrib != null)
                                {
                                    //attrib.UpgradeOpen();
                                    attrib.Layer = "MyTest";
                                }
                            }
                        }
                    }

                    //}
                }

                // Layouts iterieren
                _AcDb.DBDictionary layoutDict = (_AcDb.DBDictionary)trans.GetObject(db.LayoutDictionaryId, _AcDb.OpenMode.ForRead);
                foreach (var loEntry in layoutDict)
                {
                    if (loEntry.Key.ToUpperInvariant() == "MODEL")
                    {
                        continue;
                    }
                    _AcDb.Layout lo = (_AcDb.Layout)trans.GetObject(loEntry.Value, _AcDb.OpenMode.ForRead, false);
                    if (lo == null)
                    {
                        continue;
                    }
                    System.Diagnostics.Debug.WriteLine(string.Format(CultureInfo.CurrentCulture, "Layout: {0}", lo.LayoutName));

                    _AcDb.BlockTableRecord btRecord = (_AcDb.BlockTableRecord)trans.GetObject(lo.BlockTableRecordId, _AcDb.OpenMode.ForRead);
                    foreach (var entId in btRecord)
                    {
                        _AcDb.Entity entity = (_AcDb.Entity)trans.GetObject(entId, _AcDb.OpenMode.ForRead);

                        entity.UpgradeOpen();

                        //Access to the entity
                        //MgdAcApplication.DocumentManager.MdiActiveDocument.Editor.WriteMessage(string.Format("\nHandle: {0}", entity.Handle));
                        //System.Diagnostics.Debug.WriteLine(string.Format(CultureInfo.CurrentCulture, "Handle: {0}, Layer: {1}", entity.Handle, entity.Layer));
                        entity.Layer = "MyTest";

                        _AcDb.BlockReference block = entity as _AcDb.BlockReference;
                        if (block != null)
                        {
                            foreach (var att in block.AttributeCollection)
                            {
                                _AcDb.ObjectId           attOid = (_AcDb.ObjectId)att;
                                _AcDb.AttributeReference attrib = trans.GetObject(attOid, _AcDb.OpenMode.ForWrite) as _AcDb.AttributeReference;
                                if (attrib != null)
                                {
                                    //attrib.UpgradeOpen();
                                    attrib.Layer = "MyTest";
                                }
                            }
                        }
                    }
                }



                trans.Commit();
            }
        }