明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 8262|回复: 13

[三维] 在AutoCAD 2008中实现Loft放样

  [复制链接]
发表于 2010-5-12 09:36:00 | 显示全部楼层 |阅读模式
代码如下,注意要多添加一个引用,见附件。
  1. using System;
  2. using Autodesk.AutoCAD.ahlzl;
  3. using Autodesk.AutoCAD.DatabaseServices;
  4. using Autodesk.AutoCAD.Geometry;
  5. using Autodesk.AutoCAD.Runtime;
  6. namespace Cs_调用
  7. {
  8.     public class Class1
  9.     {
  10.         [CommandMethod("Test")]
  11.         public void Test()
  12.         {
  13.             Polyline ent1 = new Polyline();
  14.             ent1.AddVertexAt(0, new Point2d(50, 50), 0, 0, 0);
  15.             ent1.AddVertexAt(1, new Point2d(-50, 50), 0, 0, 0);
  16.             ent1.AddVertexAt(2, new Point2d(-50, -50), 0, 0, 0);
  17.             ent1.AddVertexAt(3, new Point2d(50, -50), 0, 0, 0);
  18.             ent1.Closed = true;
  19.             Circle ent2 = new Circle(new Point3d(0, 0, 200), new Vector3d(0, 0, 1), 30);
  20.             Entity[] crossEnts = new Entity[2];
  21.             crossEnts.SetValue(ent1, 0);
  22.             crossEnts.SetValue(ent2, 1);
  23.             Entity[] guideCurs = new Entity[0];
  24.             LoftOptions loftOpt = new LoftOptions();
  25.             Solid3d loftEnt = DNA.CreateLoftedSolid(crossEnts, guideCurs, null, loftOpt);
  26.             AppendEntity(loftEnt);
  27.         }
  28.         private ObjectId AppendEntity(Entity ent)
  29.         {
  30.             ObjectId entId;
  31.             Database db = HostApplicationServices.WorkingDatabase;
  32.             using (Transaction trans = db.TransactionManager.StartTransaction())
  33.             {
  34.                 BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId,
  35.                     OpenMode.ForRead);
  36.                 BlockTableRecord btr = (BlockTableRecord)trans.GetObject
  37.                     (bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
  38.                 entId = btr.AppendEntity(ent);
  39.                 trans.AddNewlyCreatedDBObject(ent, true);
  40.                 trans.Commit();
  41.             }
  42.             return entId;
  43.         }
  44.     }
  45. }

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x

评分

参与人数 2威望 +1 金钱 +5 收起 理由
李毛毛 + 5
雪山飞狐_lzh + 1 【好评】好程序

查看全部评分

发表于 2022-9-23 17:08:43 | 显示全部楼层
在哪可以转成VB.net的.
发表于 2010-5-12 09:56:00 | 显示全部楼层

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

发表于 2010-5-12 13:05:00 | 显示全部楼层
Nothing?
发表于 2010-5-12 14:14:00 | 显示全部楼层
不是nothing,如果这样的话,提示:“未将对象引用设置到对象的实例”。不知道怎么改了。
  1. Imports Autodesk.AutoCAD.ApplicationServices
  2. Imports Autodesk.AutoCAD.Colors
  3. Imports Autodesk.AutoCAD.DatabaseServices
  4. Imports Autodesk.AutoCAD.EditorInput
  5. Imports Autodesk.AutoCAD.Geometry
  6. Imports Autodesk.AutoCAD.Runtime
  7. Imports Autodesk.AutoCAD.GraphicsSystem
  8. Imports Autodesk.AutoCAD.Windows
  9. Imports Autodesk.AutoCAD.Publishing
  10. Imports Autodesk.AutoCAD.PlottingServices
  11. Imports Autodesk.AutoCAD.ComponentModel
  12. Imports Autodesk.AutoCAD.LayerManager
  13. Public Class Class1
  14.     <CommandMethod("tt")> _
  15.     Sub 天圆地方()
  16.         Try
  17.             Dim Ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
  18.             Dim db As Database = HostApplicationServices.WorkingDatabase
  19.             Dim ent1 As Polyline = New Polyline()
  20.             ent1.AddVertexAt(0, New Point2d(50, 50), 0, 0, 0)
  21.             ent1.AddVertexAt(1, New Point2d(-50, 50), 0, 0, 0)
  22.             ent1.AddVertexAt(2, New Point2d(-50, -50), 0, 0, 0)
  23.             ent1.AddVertexAt(3, New Point2d(50, -50), 0, 0, 0)
  24.             ent1.Closed = True
  25.             Dim ent2 As Circle = New Circle(New Point3d(0, 0, 200), New Vector3d(0, 0, 1), 30)
  26.             Dim crossEnts(1) As Entity
  27.             crossEnts.SetValue(ent1, 0)
  28.             crossEnts.SetValue(ent2, 1)
  29.             Dim guideCurs(0) As Entity
  30.             Dim loftOpt As LoftOptions = New LoftOptions()
  31.             Dim solid3dEnt As Solid3d = New Solid3d()
  32.             solid3dEnt.RecordHistory = True
  33.             solid3dEnt.CreateLoftedSolid(crossEnts, guideCurs, Nothing, loftOpt)
  34.             AppendEntity(ent1)
  35.         Catch ex As System.Exception
  36.             MsgBox("Error: " + ex.Message)
  37.         Finally
  38.         End Try
  39.     End Sub
  40.     Function AppendEntity(ByVal ent As Entity) As ObjectId
  41.         Dim db As Database = HostApplicationServices.WorkingDatabase
  42.         Dim entId As ObjectId
  43.         Using trans As Transaction = db.TransactionManager.StartTransaction()
  44.             Dim bt As BlockTable = trans.GetObject(db.BlockTableId, OpenMode.ForRead)
  45.             Dim Btr As BlockTableRecord = trans.GetObject(bt.Item(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
  46.             entId = Btr.AppendEntity(ent)
  47.             trans.AddNewlyCreatedDBObject(ent, True)
  48.             trans.Commit()
  49.         End Using
  50.         Return entId
  51.     End Function
  52. End Class
发表于 2010-5-12 16:32:00 | 显示全部楼层
看来,只有请ahlzl再加个函数了,呵呵
 楼主| 发表于 2010-5-12 19:03:00 | 显示全部楼层
VB.NET的代码:
  1. <CommandMethod("Test")> Public Sub Test()
  2.     Dim ent1 As New Polyline()
  3.     ent1.AddVertexAt(0, New Point2d(50, 50), 0, 0, 0)
  4.     ent1.AddVertexAt(1, New Point2d(-50, 50), 0, 0, 0)
  5.     ent1.AddVertexAt(2, New Point2d(-50, -50), 0, 0, 0)
  6.     ent1.AddVertexAt(3, New Point2d(50, -50), 0, 0, 0)
  7.     ent1.Closed = True
  8.     Dim ent2 As New Circle(New Point3d(0, 0, 200), New Vector3d(0, 0, 1), 30)
  9.     Dim crossEnts As Entity() = New Entity(1) {}
  10.     crossEnts.SetValue(ent1, 0)
  11.     crossEnts.SetValue(ent2, 1)
  12.     Dim guideCurs As Entity() = New Entity(-1) {}
  13.     Dim loftOpt As New LoftOptions()
  14.     Dim loftEnt As Solid3d = DNA.CreateLoftedSolid(crossEnts, guideCurs, Nothing, loftOpt)
  15.     AppendEntity(loftEnt)
  16. End Sub
发表于 2010-5-13 09:24:00 | 显示全部楼层
ahlzl,我太崇拜你了!!!
发表于 2010-8-13 22:09:00 | 显示全部楼层

 

明经太牛了。

请问ahlzl大侠,这段程序在C++中怎么改呢?我用的是objectarx

 楼主| 发表于 2010-8-13 22:47:00 | 显示全部楼层
C++的代码:
  1. static AcDbObjectId AppendEntity(AcDbEntity *pEnt)
  2. {
  3.     AcDbObjectId entId;
  4.     AcDbDatabase *pDb = acdbHostApplicationServices()->workingDatabase();
  5.     AcDbBlockTable *pBt;
  6.     pDb->getBlockTable(pBt, AcDb::kForRead);
  7.     AcDbBlockTableRecord *pBtr;
  8.     pBt->getAt(ACDB_MODEL_SPACE, pBtr, AcDb::kForWrite);
  9.     pBtr->appendAcDbEntity(entId, pEnt);
  10.     pBtr->close();
  11.     pBt->close();
  12.     pEnt->close();
  13.     return entId;
  14. }
  15. // - ahlzlARX_._test command (do not rename)
  16. static void ahlzlARX__test(void)
  17. {
  18.     // Add your code for command ahlzlARX_._test here
  19.     // 天圆
  20.     AcDbCircle *pCircle = new AcDbCircle(AcGePoint3d(0, 0, 20), AcGeVector3d::kZAxis, 6);
  21.     // 地方
  22.     AcGePoint2d pt1(9, 9), pt2(-9, 9), pt3(-9, -9), pt4(9, -9);
  23.     AcGePoint2dArray pts;
  24.     pts.append(pt1);
  25.     pts.append(pt2);
  26.     pts.append(pt3);
  27.     pts.append(pt4);
  28.     AcDbPolyline *pPline = new AcDbPolyline();
  29.     for (int i = 0; i < pts.length(); i++)
  30.     {
  31.         pPline->addVertexAt(i, pts[i], 0, 0, 0);
  32.     }
  33.     pPline->setClosed(Adesk::kTrue);
  34.     // 截面曲线集合
  35.     AcArray<AcDbEntity*> CrossEnts(2);           
  36.     CrossEnts.append(pPline);
  37.     CrossEnts.append(pCircle);
  38.     // 引导线
  39.     AcArray<AcDbEntity*> GuideCurs = NULL;               
  40.     // 路径曲线
  41.     AcDbEntity* pPathCur = NULL;
  42.     // 放样选项
  43.     AcDbLoftOptions LoftOpt;  
  44.     // 放样
  45.     AcDb3dSolid *solid = new AcDb3dSolid();
  46.     solid->createLoftedSolid(CrossEnts, GuideCurs, pPathCur, LoftOpt);
  47.     AcDbObjectId entId = AppendEntity(solid );
  48.     delete pCircle;
  49.     delete pPline; }
复制代码
发表于 2010-8-15 22:35:00 | 显示全部楼层
太感谢了!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-12-21 11:13 , Processed in 0.216060 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表