.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Error while changing block definition of an Block reference

6 REPLIES 6
Reply
Message 1 of 7
andrecristino
715 Views, 6 Replies

Error while changing block definition of an Block reference

Good afternoon. I'm trying to change block the definition of an block reference.

 

I did something like this:

 

blockreference.BlockTableRecord = newBlock.ObjectId;

 

In most cases this works well. However I found a case that it not work as well. When I try to change the definition from a new block that I have created in my test DWG, it disappears from the model. I wonder what is causing this problem?

6 REPLIES 6
Message 2 of 7

It would be easier to identify the problem if you could post some sample code and (ideally) also a sample DWG that causes the problem.

 

 

Cheers,

Stephen Preston
Autodesk Developer Network
Message 3 of 7

Hi.
I'm sending the DWG with the bug. The code I'm using is similar with the code below. I tried to change the block definition of my ordinary blocks to my TEST block and I had the same problem that I reported.

 

CommandMethod("CBLOCK", CommandFlags.Modal | CommandFlags.NoPaperSpace)]
public static void ChageBlock()
{
var doc = Application.DocumentManager.MdiActiveDocument;
var db = doc.Database;
var ed = doc.Editor;

var pso = new PromptSelectionOptions
{
MessageForAdding = "\nSelect block reference(s)",
AllowDuplicates = false,
AllowSubSelections = false,
ForceSubSelections = false,
RejectObjectsOnLockedLayers = true,
RejectObjectsFromNonCurrentSpace = true,
RejectPaperspaceViewport = true,
};

var selectionResult = ed.GetSelection(pso, new SelectionFilter(new[] { new TypedValue((int)DxfCode.Start, "INSERT"), }));

var options = new PromptEntityOptions("\nSelect block reference");
options.SetRejectMessage("\nSelect only block reference");
options.AddAllowedClass(typeof(BlockReference), false);

if (selectionResult.Status!= PromptStatus.OK)
return;

using (var tx = db.TransactionManager.StartTransaction())
{
//get references
var blkRefs = new ObjectIdCollection(selectionResult.Value.GetObjectIds())
.Cast<ObjectId>()
.Select(objId => tx.GetObject(objId, OpenMode.ForRead))
.OfType<BlockReference>()
.Where(x => x.BlockTableRecord.IsValid)
.ToArray();

//block table record
var blks = ((BlockTable)tx.GetObject(db.BlockTableId, OpenMode.ForRead))
.Cast<ObjectId>()
.Select(objId => tx.GetObject(objId, OpenMode.ForRead))
.OfType<BlockTableRecord>()
.Where(bk => !bk.IsFromExternalReference && (bk.Name == "*Model_Space" || !bk.Name.StartsWith("*")))
.Distinct()
.ToArray();

//Get the target block
var block = blks.First(bk => bk.Name == "TEST");

foreach (var blockReference in blkRefs)
{
blockReference.UpgradeOpen();

//set the id of the test block
blockReference.BlockTableRecord = block.ObjectId;
}

tx.Commit();
}
}

Message 4 of 7

PS: I'm using AutoCad 2013
Message 5 of 7
Balaji_Ram
in reply to: andrecristino

Hi,

 

Your code works ok for the sample drawing.

After the "BlockTableRecord" has been set, those block references overlap on each other. It appears to disappear as the position of all the block references are set to (0,0). 

 

Regards,



Balaji
Developer Technical Services
Autodesk Developer Network

Message 6 of 7

Hi Balaji.

Thanks. But now I have another problem. If you copy the reference of block "ABC", for example, and put it in some place in the drawing, there is 2 references of the same block, ok? The first reference is the one that I created the block table record, using BLOCK command, and another one is a copy from the first reference. If you run my code, you will realize that the first copy "disappears" and the second one works correctly, because it do not overlap with TEST block reference. Do you know the reason?

With best regards,

André Cristino.

Message 7 of 7
Balaji_Ram
in reply to: andrecristino

Hi André,

 

The coordinates of a block reference are relative to the containing block record origin. For this reason, you will observe a shift when the "BlockTableRecord" is modified for an existing block reference. You can understand this by trying to move the entities in the "TEST" and "ABC" blocks to the origin. Please try your code with the attached drawing that has this change.

 

Regards,



Balaji
Developer Technical Services
Autodesk Developer Network

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost