Author Topic: Why will block reference not appear in modelspace when I tell it to?  (Read 1388 times)

0 Members and 1 Guest are viewing this topic.

tonofsteel

  • Guest
Why when I get the block table record for model space and EXPLICITLY tell autocad to add a reference for a block to the block table record for model space it appears in paperspace?  Code:

Code: [Select]
           

            this.modelSpaceBlockTable = CurrentTransaction.GetObject(this.mainBlockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
            this.paperSpaceBlockTable = CurrentTransaction.GetObject(this.mainBlockTable[BlockTableRecord.PaperSpace], OpenMode.ForWrite) as BlockTableRecord;



             if (this.spaceToAddTo == AddToSpaceContext.ModelSpace)
            {
                ObjectId AppendedEntity = this.modelSpaceBlockTable.AppendEntity(newBlockReferenceToInsert);
            }
            else if (this.spaceToAddTo == AddToSpaceContext.PaperSpace)
            {
                ObjectId AppendedEntity = this.paperSpaceBlockTable.AppendEntity(newBlockReferenceToInsert);
            }

            this.currentTransaction.AddNewlyCreatedDBObject(newBlockReferenceToInsert, true);



I step through the code and it is hitting the modelspace appendentity.

The thing that I noticed is that the references are added to whichever space is active in the editor.  Please do not tell me that when programming and being able to directly reference one space or the other in the database that somehow there is some nonsense code deep in autoCAD that overrides your intentions because it thinks it knows what you really want.

tonofsteel

  • Guest
Re: Why will block reference not appear in modelspace when I tell it to?
« Reply #1 on: November 17, 2011, 02:20:58 PM »
I am trying to read in a block and then create references of it in model space.  This does not work, but if I try out some other code that creates a block in the program itself and then add a reference to modelspace it works.  I am guessing this is not working for me because there is something wrong with the way that I am inserting a block and trying to reference it?

Code: [Select]
public override List<DBObject> CustomTransactionCode()
        {
            List<DBObject> insertedBlockReferenceToCommit = new List<DBObject>();

            if (this.mainBlockTable.Has(this.blockName) == false)
            {
                using (Database tempDb = new Database(false, true))
                {
                    tempDb.ReadDwgFile(this.blockPath, FileOpenMode.OpenForReadAndAllShare, true, null);
                    ObjectId newInsertedBlock = this.currentDatabase.Insert(this.blockName, tempDb, true);

                    if (newInsertedBlock.IsNull)
                    {
                        throw new ApplicationException("Failed to read block: " + this.blockPath);
                    }
                }

            }

             
           
            BlockTableRecord existingBlock = this.mainBlockTable[blockName].GetObject(OpenMode.ForRead) as BlockTableRecord;
            BlockReference newBlockReferenceToInsert = new BlockReference(this.insertPoint, existingBlock.ObjectId);


       
            if (this.spaceToAddTo == AddToSpaceContext.ModelSpace)
            {
                ObjectId AppendedEntity = this.modelSpaceBlockTable.AppendEntity(newBlockReferenceToInsert);
            }
            else if (this.spaceToAddTo == AddToSpaceContext.PaperSpace)
            {
                ObjectId AppendedEntity = this.paperSpaceBlockTable.AppendEntity(newBlockReferenceToInsert);
            }

            this.currentTransaction.AddNewlyCreatedDBObject(newBlockReferenceToInsert, true);
           

            foreach (ObjectId ObjectIDInBlockDefinition in existingBlock)
            {
                Entity EntityInBlock = ObjectIDInBlockDefinition.GetObject(OpenMode.ForRead) as Entity;
                if (EntityInBlock.GetType() == typeof(AttributeDefinition))
                {
                    EntityInBlock.UpgradeOpen();
                    AttributeDefinition attDef = (AttributeDefinition)EntityInBlock;
                    AttributeReference attRef = new AttributeReference();

                    attRef.SetAttributeFromBlock(attDef, newBlockReferenceToInsert.BlockTransform);
                    newBlockReferenceToInsert.AttributeCollection.AppendAttribute(attRef);
                    this.currentTransaction.AddNewlyCreatedDBObject(attRef, true);
                    attRef.DowngradeOpen();
                    EntityInBlock.DowngradeOpen();
                }
            }

           
            insertedBlockReferenceToCommit.Add(newBlockReferenceToInsert);
            return insertedBlockReferenceToCommit; 
        }

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Why will block reference not appear in modelspace when I tell it to?
« Reply #2 on: November 17, 2011, 02:35:21 PM »
When you debug do modelSpaceBlockTable and paperSpaceBlockTable have the same ObjectId or name?
or do the fields match whatever the current space is?
 
 

Bryco

  • Water Moccasin
  • Posts: 1883
Re: Why will block reference not appear in modelspace when I tell it to?
« Reply #3 on: November 17, 2011, 08:20:11 PM »
If you get rid of all the "this"  does it work?