简体   繁体   中英

Programmatically get the location of a block in AutoCAD with its ObjectId

I'm trying to write a method to change an existing block in an AutoCAD drawing. In this case, I want to change the length of a block by changing its scale factors. The method I've written will accomplish that by deleting the old block and creating a new one with the newly desired scale factors.

        private static ObjectId _adjustCapPlate(ObjectId capPlateID, bool isHorizontal, Distance left = null, Distance right = null)
        {
            BlockReference capPlate = EntityMethods.GetBlockById(capPlateID); //Getting the block to be replaced
            Scale3d oldScales = capPlate.ScaleFactors; //Getting the scales of the old block
            Point3d originalLocation = capPlate.Location; // ToDo: Replace capPlate.Location with code that will return the coordinates of the insertion point of the block
            EntityMethods.DeleteBlockFromDrawingWithId(capPlate.Id); //Deleting the old block
            //Using specified splice plate length if method does not specify something else
            if (left == null)
            {
                left = new Distance(SettingsController.SplicePlateLength / 2);
            }
            if (right == null)
            {
                right = new Distance(SettingsController.SplicePlateLength);
            }

            Distance newXScale, newYScale, newZScale;
            Point3d newLocation;

            if (isHorizontal) //If wall is oriented horizontally
            {
                newXScale = new Distance(DistanceType.Inch, oldScales.X - right.Inches);
                newYScale = new Distance(DistanceType.Inch, oldScales.Y);
                newLocation = new Point3d(originalLocation.X + left.Inches, originalLocation.Y, originalLocation.Z);
            }
            else
            {
                newXScale = new Distance(DistanceType.Inch, oldScales.X);
                newYScale = new Distance(DistanceType.Inch, oldScales.Y - right.Inches);
                newLocation = new Point3d(originalLocation.X, originalLocation.Y + left.Inches, originalLocation.Z);
            }
            newZScale = new Distance(DistanceType.Inch, oldScales.Z);

            BlockReference newCapPlate = EntityMethods.InsertBlock("member", newLocation, "0", null, newXScale, newYScale, newZScale);
        }

All I need to make this method to work is to replace capPlate.Location with something that will get an existing block's XYZ coordinates in the AutoCAD drawing. It seems ridiculous that there is no obvious way to get a block's coordinates programmatically.

I will not accept answers that change my approach. This has to be done by deleting the old block and replacing it by inserting a new block with new properties where the old block was.

代替使用capPlate.Location ,使用capPlate.Position ,您应该获得所需的行为。

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