CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

AutoCAD 2022 开发者帮助

缩放对象 (.NET)

2024-5-18 18:48| 发布者: admin| 查看: 102| 评论: 0|原作者: admin|来自: AutoCAD

缩放对象 (.NET)

通过基于当前绘图单位指定基点和比例因子来缩放对象。可以缩放所有图形对象以及属性参照对象。

若要缩放对象,请使用转换矩阵的函数。此函数需要对象比例因子的数值和缩放操作基点的对象。该函数在 XYZ 方向上平均缩放对象。对象的尺寸乘以比例因子。大于 1 的比例因子将放大对象。介于 0 和 1 之间的比例因子会减少对象。ScalingPoint3dScaling

注意:如果需要非均匀缩放对象,则需要使用适当的数据数组初始化转换矩阵,然后使用对象的方法。TransformBy

缩放折线

本示例创建闭合的轻量级折线,然后从基点 (4,4.25,0) 按折线比例缩放 0.5。

VB.NET

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
 
<CommandMethod("ScaleObject")> _
Public Sub ScaleObject()
    '' Get the current document and database
    Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
    Dim acCurDb As Database = acDoc.Database

    '' Start a transaction
    Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()

        '' Open the Block table for read
        Dim acBlkTbl As BlockTable
        acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, _
                                     OpenMode.ForRead)

        '' Open the Block table record Model space for write
        Dim acBlkTblRec As BlockTableRecord
        acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _
                                        OpenMode.ForWrite)

        '' Create a lightweight polyline
        Using acPoly As Polyline = New Polyline()
            acPoly.AddVertexAt(0, New Point2d(1, 2), 0, 0, 0)
            acPoly.AddVertexAt(1, New Point2d(1, 3), 0, 0, 0)
            acPoly.AddVertexAt(2, New Point2d(2, 3), 0, 0, 0)
            acPoly.AddVertexAt(3, New Point2d(3, 3), 0, 0, 0)
            acPoly.AddVertexAt(4, New Point2d(4, 4), 0, 0, 0)
            acPoly.AddVertexAt(5, New Point2d(4, 2), 0, 0, 0)

            '' Close the polyline
            acPoly.Closed = True

            '' Reduce the object by a factor of 0.5 
            '' using a base point of (4,4.25,0)
            acPoly.TransformBy(Matrix3d.Scaling(0.5, New Point3d(4, 4.25, 0)))

            '' Add the new object to the block table record and the transaction
            acBlkTblRec.AppendEntity(acPoly)
            acTrans.AddNewlyCreatedDBObject(acPoly, True)
        End Using

        '' Save the new objects to the database
        acTrans.Commit()
    End Using
End Sub

C#

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
 
[CommandMethod("ScaleObject")]
public static void ScaleObject()
{
    // Get the current document and database
    Document acDoc = Application.DocumentManager.MdiActiveDocument;
    Database acCurDb = acDoc.Database;

    // Start a transaction
    using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
    {
        // Open the Block table for read
        BlockTable acBlkTbl;
        acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                                     OpenMode.ForRead) as BlockTable;

        // Open the Block table record Model space for write
        BlockTableRecord acBlkTblRec;
        acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                                        OpenMode.ForWrite) as BlockTableRecord;

        // Create a lightweight polyline
        using (Polyline acPoly = new Polyline())
        {
            acPoly.AddVertexAt(0, new Point2d(1, 2), 0, 0, 0);
            acPoly.AddVertexAt(1, new Point2d(1, 3), 0, 0, 0);
            acPoly.AddVertexAt(2, new Point2d(2, 3), 0, 0, 0);
            acPoly.AddVertexAt(3, new Point2d(3, 3), 0, 0, 0);
            acPoly.AddVertexAt(4, new Point2d(4, 4), 0, 0, 0);
            acPoly.AddVertexAt(5, new Point2d(4, 2), 0, 0, 0);

            // Close the polyline
            acPoly.Closed = true;

            // Reduce the object by a factor of 0.5 
            // using a base point of (4,4.25,0)
            acPoly.TransformBy(Matrix3d.Scaling(0.5, new Point3d(4, 4.25, 0)));

            // Add the new object to the block table record and the transaction
            acBlkTblRec.AppendEntity(acPoly);
            acTrans.AddNewlyCreatedDBObject(acPoly, true);
        }

        // Save the new objects to the database
        acTrans.Commit();
    }
}

VBA/ActiveX 代码参考

Sub ScaleObject()
    ' Create the polyline
    Dim plineObj As AcadLWPolyline
    Dim points(0 To 11) As Double
    points(0) = 1: points(1) = 2
    points(2) = 1: points(3) = 3
    points(4) = 2: points(5) = 3
    points(6) = 3: points(7) = 3
    points(8) = 4: points(9) = 4
    points(10) = 4: points(11) = 2
    Set plineObj = ThisDrawing.ModelSpace. _
                                 AddLightWeightPolyline(points)
    plineObj.Closed = True
    ZoomAll
 
    ' Define the scale
    Dim basePoint(0 To 2) As Double
    Dim scalefactor As Double
    basePoint(0) = 4: basePoint(1) = 4.25: basePoint(2) = 0
    scalefactor = 0.5
 
    ' Scale the polyline
    plineObj.ScaleEntity basePoint, scalefactor
    plineObj.Update
End Sub

路过

雷人

握手

鲜花

鸡蛋

最新评论

QQ|Archiver|CAD开发者社区 ( 苏ICP备2022047690号-1   苏公网安备32011402011833)

GMT+8, 2024-12-15 11:43

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部