以编程方式获取块在 AutoCAD 中的位置及其 ObjectId

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

我正在尝试编写一种方法来更改 AutoCAD 绘图中的现有块。在这种情况下,我想通过改变其比例因子来改变块的长度。我编写的方法将通过删除旧块并使用新的所需比例因子创建一个新块来实现这一点。

        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);
        }

要使此方法起作用,我需要做的就是将 capPlate.Location 替换为可以在 AutoCAD 绘图中获取现有块的 XYZ 坐标的内容。没有明显的方法以编程方式获取块的坐标,这似乎很荒谬。

我不会接受改变我方法的答案。这必须通过删除旧块并通过在旧块所在的位置插入具有新属性的新块来替换它来完成。

不使用 capPlate.Location,而是使用 capPlate.Position,您应该会得到想要的行为。