예제 #1
0
        public static ObjectId AppendBlockItem(Point3d insertPointWcs, ObjectId blockTableRecordId,
                                               Dictionary <string, string> attrTextValues)
        {
            ObjectId resBlockId = ObjectId.Null;

            Tools.StartTransaction(() =>
            {
                Transaction trans = Tools.GetTopTransaction();

                // Add a block reference to the model space
                BlockTableRecord ms = Tools.GetAcadBlockTableRecordModelSpace(OpenMode.ForWrite);

                BlockTableRecord btr = blockTableRecordId.GetObjectForRead <BlockTableRecord>();

                BlockReference br = new BlockReference(insertPointWcs, blockTableRecordId);
                br.SetDatabaseDefaults();

                ObjectContextManager ocm    = btr.Database.ObjectContextManager;
                ObjectContextCollection occ = ocm.GetContextCollection("ACDB_ANNOTATIONSCALES");

                if (btr.Annotative == AnnotativeStates.True)
                {
                    br.AddContext(occ.CurrentContext);
                }

                resBlockId = ms.AppendEntity(br);
                trans.AddNewlyCreatedDBObject(br, true);

                // Add attributes from the block table record
                List <AttributeDefinition> attributes = GetAttributes(btr, trans);

                foreach (AttributeDefinition acAtt in attributes)
                {
                    acAtt.UpgradeOpen();
                    acAtt.AdjustAlignment(br.Database); //
                    acAtt.RecordGraphicsModified(true); //

                    if (!acAtt.Constant)
                    {
                        using (AttributeReference acAttRef = new AttributeReference())
                        {
                            acAttRef.SetAttributeFromBlock(acAtt, br.BlockTransform);

                            if (attrTextValues != null)
                            {
                                if (attrTextValues.ContainsKey(acAtt.Tag))
                                {
                                    acAttRef.TextString = attrTextValues[acAtt.Tag];
                                }
                                else
                                {
                                    acAttRef.TextString = acAtt.TextString;
                                }
                            }
                            else
                            {
                                acAttRef.TextString = acAtt.TextString;
                            }

                            acAttRef.AdjustAlignment(br.Database);  //
                            acAttRef.RecordGraphicsModified(true);  //

                            br.AttributeCollection.AppendAttribute(acAttRef);
                            trans.AddNewlyCreatedDBObject(acAttRef, true);
                        }
                    }
                }
                br.RecordGraphicsModified(true);
            });
            return(resBlockId);
        }