ahlzl 发表于 2010-5-12 09:36

在AutoCAD 2008中实现Loft放样

代码如下,注意要多添加一个引用,见附件。

using System;
using Autodesk.AutoCAD.ahlzl;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
namespace Cs_调用
{
    public class Class1
    {
      
      public void Test()
      {
            Polyline ent1 = new Polyline();
            ent1.AddVertexAt(0, new Point2d(50, 50), 0, 0, 0);
            ent1.AddVertexAt(1, new Point2d(-50, 50), 0, 0, 0);
            ent1.AddVertexAt(2, new Point2d(-50, -50), 0, 0, 0);
            ent1.AddVertexAt(3, new Point2d(50, -50), 0, 0, 0);
            ent1.Closed = true;
            Circle ent2 = new Circle(new Point3d(0, 0, 200), new Vector3d(0, 0, 1), 30);
            Entity[] crossEnts = new Entity;
            crossEnts.SetValue(ent1, 0);
            crossEnts.SetValue(ent2, 1);
            Entity[] guideCurs = new Entity;
            LoftOptions loftOpt = new LoftOptions();
            Solid3d loftEnt = DNA.CreateLoftedSolid(crossEnts, guideCurs, null, loftOpt);
            AppendEntity(loftEnt);
      }
      private ObjectId AppendEntity(Entity ent)
      {
            ObjectId entId;
            Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId,
                  OpenMode.ForRead);
                BlockTableRecord btr = (BlockTableRecord)trans.GetObject
                  (bt, OpenMode.ForWrite);
                entId = btr.AppendEntity(ent);
                trans.AddNewlyCreatedDBObject(ent, true);
                trans.Commit();
            }
            return entId;
      }
    }
}

Linxian1028 发表于 2022-9-23 17:08

在哪可以转成VB.net的.

Crazyhoof 发表于 2010-5-12 09:56

<p>请问楼主,在vb.net中不支持null。solid3dEnt.CreateLoftedSolid(crossEnts, guideCurs, Null, loftOpt)&nbsp; 这句话要怎么改呢?</p>

雪山飞狐_lzh 发表于 2010-5-12 13:05

Nothing?

Crazyhoof 发表于 2010-5-12 14:14

不是nothing,如果这样的话,提示:“未将对象引用设置到对象的实例”。不知道怎么改了。


Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.Colors
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.GraphicsSystem
Imports Autodesk.AutoCAD.Windows
Imports Autodesk.AutoCAD.Publishing
Imports Autodesk.AutoCAD.PlottingServices
Imports Autodesk.AutoCAD.ComponentModel
Imports Autodesk.AutoCAD.LayerManager
Public Class Class1
    <CommandMethod("tt")> _
    Sub 天圆地方()
      Try
            Dim Ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
            Dim db As Database = HostApplicationServices.WorkingDatabase
            Dim ent1 As Polyline = New Polyline()
            ent1.AddVertexAt(0, New Point2d(50, 50), 0, 0, 0)
            ent1.AddVertexAt(1, New Point2d(-50, 50), 0, 0, 0)
            ent1.AddVertexAt(2, New Point2d(-50, -50), 0, 0, 0)
            ent1.AddVertexAt(3, New Point2d(50, -50), 0, 0, 0)
            ent1.Closed = True
            Dim ent2 As Circle = New Circle(New Point3d(0, 0, 200), New Vector3d(0, 0, 1), 30)
            Dim crossEnts(1) As Entity
            crossEnts.SetValue(ent1, 0)
            crossEnts.SetValue(ent2, 1)
            Dim guideCurs(0) As Entity
            Dim loftOpt As LoftOptions = New LoftOptions()
            Dim solid3dEnt As Solid3d = New Solid3d()
            solid3dEnt.RecordHistory = True
            solid3dEnt.CreateLoftedSolid(crossEnts, guideCurs, Nothing, loftOpt)
            AppendEntity(ent1)
      Catch ex As System.Exception
            MsgBox("Error: " + ex.Message)
      Finally
      End Try
    End Sub
    Function AppendEntity(ByVal ent As Entity) As ObjectId
      Dim db As Database = HostApplicationServices.WorkingDatabase
      Dim entId As ObjectId
      Using trans As Transaction = db.TransactionManager.StartTransaction()
            Dim bt As BlockTable = trans.GetObject(db.BlockTableId, OpenMode.ForRead)
            Dim Btr As BlockTableRecord = trans.GetObject(bt.Item(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
            entId = Btr.AppendEntity(ent)
            trans.AddNewlyCreatedDBObject(ent, True)
            trans.Commit()
      End Using
      Return entId
    End Function
End Class

雪山飞狐_lzh 发表于 2010-5-12 16:32

看来,只有请ahlzl再加个函数了,呵呵

ahlzl 发表于 2010-5-12 19:03

VB.NET的代码:

<CommandMethod("Test")> Public Sub Test()
    Dim ent1 As New Polyline()
    ent1.AddVertexAt(0, New Point2d(50, 50), 0, 0, 0)
    ent1.AddVertexAt(1, New Point2d(-50, 50), 0, 0, 0)
    ent1.AddVertexAt(2, New Point2d(-50, -50), 0, 0, 0)
    ent1.AddVertexAt(3, New Point2d(50, -50), 0, 0, 0)
    ent1.Closed = True
    Dim ent2 As New Circle(New Point3d(0, 0, 200), New Vector3d(0, 0, 1), 30)
    Dim crossEnts As Entity() = New Entity(1) {}
    crossEnts.SetValue(ent1, 0)
    crossEnts.SetValue(ent2, 1)
    Dim guideCurs As Entity() = New Entity(-1) {}
    Dim loftOpt As New LoftOptions()
    Dim loftEnt As Solid3d = DNA.CreateLoftedSolid(crossEnts, guideCurs, Nothing, loftOpt)
    AppendEntity(loftEnt)
End Sub

Crazyhoof 发表于 2010-5-13 09:24

<strong><font face="Verdana" color="#da2549">ahlzl,我太崇拜你了!!!</font></strong>

ivanbenjamin 发表于 2010-8-13 22:09

<p>&nbsp; </p>
<p>明经太牛了。</p>
<p>请问ahlzl大侠,这段程序在C++中怎么改呢?我用的是objectarx</p>

ahlzl 发表于 2010-8-13 22:47

C++的代码:

static AcDbObjectId AppendEntity(AcDbEntity *pEnt)
{
    AcDbObjectId entId;
    AcDbDatabase *pDb = acdbHostApplicationServices()->workingDatabase();
    AcDbBlockTable *pBt;
    pDb->getBlockTable(pBt, AcDb::kForRead);
    AcDbBlockTableRecord *pBtr;
    pBt->getAt(ACDB_MODEL_SPACE, pBtr, AcDb::kForWrite);
    pBtr->appendAcDbEntity(entId, pEnt);
    pBtr->close();
    pBt->close();
    pEnt->close();
    return entId;
}

// - ahlzlARX_._test command (do not rename)
static void ahlzlARX__test(void)
{
    // Add your code for command ahlzlARX_._test here
    // 天圆
    AcDbCircle *pCircle = new AcDbCircle(AcGePoint3d(0, 0, 20), AcGeVector3d::kZAxis, 6);

    // 地方
    AcGePoint2d pt1(9, 9), pt2(-9, 9), pt3(-9, -9), pt4(9, -9);
    AcGePoint2dArray pts;
    pts.append(pt1);
    pts.append(pt2);
    pts.append(pt3);
    pts.append(pt4);

    AcDbPolyline *pPline = new AcDbPolyline();
    for (int i = 0; i < pts.length(); i++)
    {
      pPline->addVertexAt(i, pts, 0, 0, 0);
    }
    pPline->setClosed(Adesk::kTrue);

    // 截面曲线集合
    AcArray<AcDbEntity*> CrossEnts(2);         
    CrossEnts.append(pPline);
    CrossEnts.append(pCircle);

    // 引导线
    AcArray<AcDbEntity*> GuideCurs = NULL;               

    // 路径曲线
    AcDbEntity* pPathCur = NULL;

    // 放样选项
    AcDbLoftOptions LoftOpt;

    // 放样
    AcDb3dSolid *solid = new AcDb3dSolid();
    solid->createLoftedSolid(CrossEnts, GuideCurs, pPathCur, LoftOpt);
    AcDbObjectId entId = AppendEntity(solid );
    delete pCircle;
    delete pPline; }

ivanbenjamin 发表于 2010-8-15 22:35

太感谢了!
页: [1] 2
查看完整版本: 在AutoCAD 2008中实现Loft放样