예제 #1
0
        addBlockRef(string strName, Point3d pnt3dIns, double dblRotation, List <string> attValues)
        {
            Database db = BaseObjs._db;
            Editor   ed = BaseObjs._editor;

            BlockTableRecord btrx = null;
            BlockReference   br   = null;

            try
            {
                using (Transaction tr = BaseObjs.startTransactionDb())
                {
                    BlockTable bt = (BlockTable)db.BlockTableId.GetObject(OpenMode.ForRead);
                    if (!bt.Has(strName))
                    {
                        btrx = addBtr(strName);
                    }
                    else
                    {
                        btrx = (BlockTableRecord)bt[strName].GetObject(OpenMode.ForRead);
                    }

                    //---> debug only
                    foreach (ObjectId idObj in btrx)
                    {
                        Entity ent             = (Entity)idObj.GetObject(OpenMode.ForRead);
                        AttributeDefinition ad = ent as AttributeDefinition;

                        if (ad != null)
                        {
                            ed.WriteMessage(string.Format("\n{0}", ad.Tag));
                        }
                    }//<--- debug only

                    //BlockTableRecord Btr = (BlockTableRecord)DB.CurrentSpaceId.GetObject(OpenMode.ForWrite);

                    btrx.UpgradeOpen();

                    using (btrx)
                    {
                        br = new BlockReference(pnt3dIns, btrx.ObjectId);
                        using (br)
                        {
                            Matrix3d           ucsMatrix = ed.CurrentUserCoordinateSystem;
                            CoordinateSystem3d ucs       = ucsMatrix.CoordinateSystem3d;

                            Matrix3d mat3d = new Matrix3d();
                            mat3d = Matrix3d.Rotation(dblRotation, ucs.Zaxis, pnt3dIns);

                            br.TransformBy(mat3d);
                            br.ScaleFactors = new Scale3d(1, 1, 1);
                            btrx.AppendEntity(br);
                            tr.AddNewlyCreatedDBObject(br, true);

                            BlockTableRecord btratt = (BlockTableRecord)br.BlockTableRecord.GetObject(OpenMode.ForRead);
                            using (btratt)
                            {
                                Autodesk.AutoCAD.DatabaseServices.AttributeCollection ATTcol = br.AttributeCollection;

                                foreach (ObjectId subid in btratt)
                                {
                                    Entity ent             = (Entity)subid.GetObject(OpenMode.ForRead);
                                    AttributeDefinition ad = ent as AttributeDefinition;

                                    if (ad != null)
                                    {
                                        AttributeReference ar = new AttributeReference();
                                        ar.SetPropertiesFrom(ad);
                                        ar.SetAttributeFromBlock(ad, br.BlockTransform);
                                        ar.Visible = ad.Visible;

                                        ar.HorizontalMode = ad.HorizontalMode;
                                        ar.VerticalMode   = ad.VerticalMode;
                                        ar.Rotation       = ad.Rotation;
                                        ar.TextStyleId    = ad.TextStyleId;
                                        ar.Position       = ad.Position + pnt3dIns.GetAsVector();
                                        ar.Tag            = ad.Tag;
                                        ar.FieldLength    = ad.FieldLength;
                                        ar.AdjustAlignment(db);

                                        //if (ar.Tag == "TOPTXT")
                                        //    ar.TextString = strTop;

                                        //if (ar.Tag == "MIDTXT")
                                        //    ar.TextString = strMid;

                                        //if (ar.Tag == "BOTTXT")
                                        //    ar.TextString = strBot;

                                        ar.Position = ad.Position.TransformBy(br.BlockTransform);

                                        ATTcol.AppendAttribute(ar);
                                        tr.AddNewlyCreatedDBObject(ar, true);
                                    }
                                }
                            }
                            br.DowngradeOpen();
                        }

                        btrx.DowngradeOpen();
                    }
                    tr.Commit();
                }
            }
            catch (System.Exception ex)
            {
                BaseObjs.writeDebug(ex.Message + " Blocks.cs: line: 194");
            }
            return(br);
        }
예제 #2
0
        addBlockRef(string strName, string strTopNum, string strTopTxt, string strBotNum, string strBotTxt, Point3d pnt3dIns, double dblRotation)
        {
            Database DB = BaseObjs._db;
            Editor   ED = BaseObjs._editor;

            ObjectId         blkID = ObjectId.Null;
            BlockTableRecord Btrx  = null;

            using (Transaction tr = BaseObjs.startTransactionDb())
            {
                BlockTable BT = (BlockTable)DB.BlockTableId.GetObject(OpenMode.ForRead);
                if (!BT.Has(strName))
                {
                    blkID = insBlockRef(@"R:\Tset\Block\", "GradeTag.dwg");
                    Btrx  = (BlockTableRecord)blkID.GetObject(OpenMode.ForRead);
                    Btrx.UpgradeOpen();
                    Btrx.Name = "GradeTag";
                    Btrx.DowngradeOpen();
                }
                else
                {
                    Btrx = (BlockTableRecord)BT[strName].GetObject(OpenMode.ForRead);
                }

                //---> debug only
                foreach (ObjectId objID in Btrx)
                {
                    Entity ENT             = (Entity)objID.GetObject(OpenMode.ForRead);
                    AttributeDefinition AD = ENT as AttributeDefinition;

                    if (AD != null)
                    {
                        ED.WriteMessage(string.Format("\n{0}", AD.Tag));
                    }
                }//<--- debug only

                BlockTableRecord Btr = (BlockTableRecord)DB.CurrentSpaceId.GetObject(OpenMode.ForWrite);

                using (Btr)
                {
                    BlockReference BR = new BlockReference(pnt3dIns, Btrx.ObjectId);
                    using (BR)
                    {
                        Matrix3d           UCSMatrix = ED.CurrentUserCoordinateSystem;
                        CoordinateSystem3d UCS       = UCSMatrix.CoordinateSystem3d;

                        Matrix3d MAT3d = new Matrix3d();
                        MAT3d = Matrix3d.Rotation(dblRotation, UCS.Zaxis, pnt3dIns);

                        BR.TransformBy(MAT3d);
                        BR.ScaleFactors = new Scale3d(1, 1, 1);
                        Btr.AppendEntity(BR);
                        tr.AddNewlyCreatedDBObject(BR, true);

                        BlockTableRecord Btratt = (BlockTableRecord)BR.BlockTableRecord.GetObject(OpenMode.ForRead);
                        using (Btratt)
                        {
                            Autodesk.AutoCAD.DatabaseServices.AttributeCollection ATTcol = BR.AttributeCollection;

                            foreach (ObjectId subID in Btratt)
                            {
                                Entity ENT             = (Entity)subID.GetObject(OpenMode.ForRead);
                                AttributeDefinition AD = ENT as AttributeDefinition;

                                if (AD != null)
                                {
                                    AttributeReference AR = new AttributeReference();
                                    AR.SetPropertiesFrom(AD);
                                    AR.SetAttributeFromBlock(AD, BR.BlockTransform);
                                    AR.Visible = AD.Visible;

                                    AR.HorizontalMode = AD.HorizontalMode;
                                    AR.VerticalMode   = AD.VerticalMode;
                                    AR.Rotation       = AD.Rotation;
                                    AR.TextStyleId    = AD.TextStyleId;
                                    AR.Position       = AD.Position + pnt3dIns.GetAsVector();
                                    AR.Tag            = AD.Tag;
                                    AR.FieldLength    = AD.FieldLength;
                                    AR.AdjustAlignment(DB);

                                    if (AR.Tag == "TOPNUM")
                                    {
                                        AR.TextString = strTopNum;
                                    }

                                    if (AR.Tag == "TOPTXT")
                                    {
                                        AR.TextString = strTopTxt;
                                    }

                                    if (AR.Tag == "BOTNUM")
                                    {
                                        AR.TextString = strBotNum;
                                    }

                                    if (AR.Tag == "BOTTXT")
                                    {
                                        AR.TextString = strBotTxt;
                                    }

                                    AR.Position = AD.Position.TransformBy(BR.BlockTransform);

                                    ATTcol.AppendAttribute(AR);
                                    tr.AddNewlyCreatedDBObject(AR, true);
                                } // end if
                            }     //end foreach
                        }
                        BR.DowngradeOpen();
                    }

                    Btr.DowngradeOpen();
                }

                // BT.DowngradeOpen ();
                tr.Commit();
            }
            return(true);
        }