简体   繁体   中英

Searching for a specific block when multiple blocks have the same name

Good Day,

I've got the problem where the c# program inserts 10 blocks (Via a matrix transform method) onto the drawing at various different points the user is prompted to identify.

The blocks are identical in terms of name and attributes, the only thing that would be different about them is their X and Y values.

I need a way to change the attributes of some of the blocks at user prompt, for instance block 7 and only block 7. Without changing the identically named attributes of the other 9 identical blocks.

Is there a way to scan through all the blocks and find a block based specifically on their X and Y value? Then edit the attributes of that specific block.

EDIT:

I added the blocks using the method code below, the path drawing contains only that one block.

What I need to do now is change the text attributes of that block, which means I need my code to find it. I considered using a selection method but I'd prefer to have this part of the code automated and would rather not have the user having to select the required blocks.

I could have the code scan all the blocks for blocks of that specific name and then scan again through those blocks for the block at the required X and Y insertion point positions (Is that possible?)(My program deals with the X and Y positions earlier therefore I know the points, don't need user to give them again). For sake of question call them BlkPt1 - BlkPt10.

But the scan all blocks method may be a bit intense on larger drawings with many blocks, is there a shorter way of doing this? I know one solution would be to change the way I insert the blocks but I really hate the usual blockreference / blocktablerecord insertion method. Is there no other way?

    using (tr)
    {
        Vector3d blockDisplacement1 = new Vector3d(ptInsert.Value.X, ptInsert.Value.Y, ptInsert.Value.Z);

        double blockScale = 1.0;

        string blockPath = @"T:\test.dwg";

        Database tmpDb = new Database(false, true);
        tmpDb.ReadDwgFile(blockPath, System.IO.FileShare.Read, true, "");

        Matrix3d Transform = Matrix3d
                    .Scaling(blockScale, Point3d.Origin)
                    .PreMultiplyBy(Matrix3d.Displacement(blockDisplacement1))
                    .PreMultiplyBy(ed.CurrentUserCoordinateSystem);

        db.Insert(Transform, tmpDb, true);

        tr.Commit();
        tr.Dispose();
    }
}

Keep track of the .Handle assigned to the block after it is inserted - that is the unique identifier for an object in an AutoCAD database. It is unique only to that specific database, it is not globally unique. Don't try to use the .ObjectId to track this, it changes between sessions, .Handle is consistent.

Make sure the AttributeReference you want to change is not a .Constant because it will be the same for every reference to that BlockDefinition .

tr.Dispose(); is not necessary inside a using () {} statement, just tr.Commit(); is fine.

See the Wiki for more links for AutoCAD code stuff.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM