找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 2593|回复: 3

[分享] EntLast EntNext

[复制链接]

已领礼包: 859个

财富等级: 财运亨通

发表于 2014-5-10 18:46:52 来自手机 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

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

×
本帖最后由 csharp 于 2014-5-11 08:19 编辑

http://www.theswamp.org/index.php?topic=37409.50;wap2

I'd guess that your translation may be skewey.

Can you post the VB.net code you used for each or post he solution zipped so I can test it here.


huiz:
I thought they did work in C# but unfortunately not in vb. Here are the examples in vb:


--- Code: ---<CommandMethod("InsDXF7")> _
Public Sub InsDXF7()
        Dim doc As Document = AcadApp.DocumentManager.MdiActiveDocument
        Dim ed As Editor = doc.Editor
        Dim db As Database = doc.Database
        Dim tmpDb As New Database(False, True)

        tmpDb.DxfIn("K:\ToTest\Squircle.dxf", "K:\ToTest\Squircle.log")
        Dim pnt1 As New Point3d(0, 0, 0)
        Dim pnt2 As New Point3d(1000, 1000, 0)
        Dim scale As Double = 10.0
        Dim vector As New Vector3d(0, 0, 0)

        Dim transform As Matrix3d = Matrix3d.Scaling(scale, pnt2) * Matrix3d.Displacement(pnt1.GetVectorTo(pnt2))
        Dim lastEnt As ObjectId = AcUtils.EntLast()
        Dim newColor As Autodesk.AutoCAD.Colors.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByAci, 1)

        db.Insert(transform, tmpDb, True)

        Using tr As Transaction = doc.TransactionManager.StartTransaction()
        ' if the db was empty lastent will be null        
        Try
        If lastEnt = ObjectId.Null Then
        lastEnt = AcUtils.EntFirst()
        idColl.Add(lastEnt)
        End If
        Dim nextent As ObjectId = AcUtils.EntNext(lastEnt)
        While nextent <> ObjectId.Null
        idColl.Add(nextent)
        nextent = AcUtils.EntNext(nextent)
        End While

        For Each id As ObjectId In idColl
        Dim acEntity As Entity = DirectCast(tr.GetObject(id, OpenMode.ForWrite), Entity)
        acEntity.Color = newColor
        Next
        Catch exx As Autodesk.AutoCAD.Runtime.Exception
        ed.WriteMessage(vbLf & exx.ToString())
        End Try
        tr.Commit()
        End Using
End Sub

--- End code ---


--- Code: ---<CommandMethod("InsDXF8")> _
Public Sub InsDXF8()
        Dim doc As Document = AcadApp.DocumentManager.MdiActiveDocument
        Dim ed As Editor = doc.Editor
        Dim db As Database = doc.Database
        Dim tmpDb As New Database(False, True)

        tmpDb.DxfIn("K:\ToTest\Squircle.dxf", "K:\ToTest\Squircle.log")
        Dim pnt1 As New Point3d(0, 0, 0)
        Dim pnt2 As New Point3d(1000, 1000, 0)
        Dim scale As Double = 10.0
        Dim vector As New Vector3d(0, 0, 0)

        Dim transform As Matrix3d = Matrix3d.Scaling(scale, pnt2) * Matrix3d.Displacement(pnt1.GetVectorTo(pnt2))
        Dim idColl As New ObjectIdCollection()
        Dim lastEnt As ObjectId = AcUtils.EntLast()
        Dim acEntity As Entity
        Dim newColor As Autodesk.AutoCAD.Colors.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByAci, 1)

        db.Insert(transform, tmpDb, True)

        Using tr As Transaction = doc.TransactionManager.StartTransaction()
        ' if the db was empty lastent will be null   
        Try
        If lastEnt = ObjectId.Null Then
        lastEnt = AcUtils.EntFirst()
        acEntity = DirectCast(tr.GetObject(lastEnt, OpenMode.ForWrite), Entity)
        acEntity.Color = newColor
        End If
        Dim nextent As ObjectId = AcUtils.EntNext(lastEnt)

        While nextent <> ObjectId.Null
        acEntity = DirectCast(tr.GetObject(nextent, OpenMode.ForWrite), Entity)
        acEntity.Color = newColor
        nextent = AcUtils.EntNext(nextent)
        End While
        Catch exx As Autodesk.AutoCAD.Runtime.Exception
        ed.WriteMessage(vbLf & exx.ToString())
        End Try
        tr.Commit()
        End Using
End Sub

--- End code ---


--- Code: ---<CommandMethod("InsDXF9")> _
Public Sub InsDXF9()
        Dim doc As Document = AcadApp.DocumentManager.MdiActiveDocument
        Dim ed As Editor = doc.Editor
        Dim db As Database = doc.Database
        Dim tmpDb As New Database(False, True)

        tmpDb.DxfIn("K:\ToTest\Squircle.dxf", "K:\ToTest\Squircle.log")
        Dim pnt1 As New Point3d(0, 0, 0)
        Dim pnt2 As New Point3d(1000, 1000, 0)
        Dim scale As Double = 10.0
        Dim vector As New Vector3d(0, 0, 0)

        Dim transform As Matrix3d = Matrix3d.Scaling(scale, pnt2) * Matrix3d.Displacement(pnt1.GetVectorTo(pnt2))
        Dim idColl As New ObjectIdCollection()
        Dim acEntity As Entity
        Dim newColor As Autodesk.AutoCAD.Colors.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByAci, 1)

        Using tr As Transaction = tmpDb.TransactionManager.StartTransaction()
        Try
        Dim tmpDbBlockTable As BlockTable = DirectCast(tr.GetObject(tmpDb.BlockTableId, OpenMode.ForRead), BlockTable)
        Dim tmpDbModelSpace As BlockTableRecord = DirectCast(tr.GetObject(tmpDbBlockTable(BlockTableRecord.ModelSpace), OpenMode.ForWrite), BlockTableRecord)

        For Each id As ObjectId In tmpDbModelSpace
        acEntity = DirectCast(tr.GetObject(id, OpenMode.ForWrite), Entity)
        acEntity.Color = newColor
        Next
        tr.Commit()
        Catch exx As Autodesk.AutoCAD.Runtime.Exception
        ed.WriteMessage(vbLf & exx.ToString())
        End Try
        End Using

        db.Insert(transform, tmpDb, True)

End Sub

--- End code ---


Kerry:
Give these a run ...
(Code solution .ZIP attached)




--- Code: ---
Option Explicit On
Option Strict On

Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.EditorInput

Imports Autodesk.AutoCAD.Colors
Imports Utils = Autodesk.AutoCAD.Internal.Utils

<Assembly: CommandClass(GetType(InsertDXF.MyCommands))>

Namespace InsertDXF

    Public Class MyCommands


   '' < snip >

    End Class

End Namespace

--- End code ---


--- Code: ---
        <CommandMethod("InsDXF7")> _
        Public Sub InsDXF7()
            Dim doc As Document = Application.DocumentManager.MdiActiveDocument
            Dim ed As Editor = doc.Editor
            Dim db As Database = doc.Database
            Dim tmpDb As New Database(False, True)
            tmpDb.DxfIn("K:\\ToTest\\Squircle.dxf", "K:\ToTest\\Squircle.log")
            Dim pnt1 As New Point3d(0, 0, 0)
            Dim pnt2 As New Point3d(1000, 1000, 0)
            Dim scale As Double = 10
            Dim vector As New Vector3d(0, 0, 0)
            Dim transform As Matrix3d = (Matrix3d.Scaling(scale, pnt2) _
                                         * Matrix3d.Displacement(pnt1.GetVectorTo(pnt2)))
            Dim idColl As New ObjectIdCollection
            Dim lastEnt As ObjectId = Utils.EntLast
            Dim newColor As Color = Color.FromColorIndex(ColorMethod.ByAci, 1)
            db.Insert(transform, tmpDb, True)
            Using tr As Transaction = doc.TransactionManager.StartTransaction
                Try
                    If (lastEnt = ObjectId.Null) Then
                        lastEnt = Utils.EntFirst
                        idColl.Add(lastEnt)
                    End If

                    Dim nextent As ObjectId = Utils.EntNext(lastEnt)
                    Do While (nextent <> ObjectId.Null)
                        idColl.Add(nextent)
                        nextent = Utils.EntNext(nextent)
                    Loop

                    Dim id As ObjectId
                    For Each id In idColl
                        Dim acEntity As Entity = DirectCast(tr.GetObject( _
                                id, OpenMode.ForWrite), Entity)
                        acEntity.Color = newColor
                    Next

                Catch exx As Autodesk.AutoCAD.Runtime.Exception
                    ed.WriteMessage((ChrW(10) & exx.ToString))
                End Try
                tr.Commit()
            End Using
        End Sub

--- End code ---



--- Code: ---
        <CommandMethod("InsDXF8")> _
        Public Sub InsDXF8()
            Dim doc As Document = Application.DocumentManager.MdiActiveDocument
            Dim ed As Editor = doc.Editor
            Dim db As Database = doc.Database
            Dim tmpDb As New Database(False, True)
            tmpDb.DxfIn("K:\\ToTest\\Squircle.dxf", "K:\\ToTest\\Squircle.log")
            Dim pnt1 As New Point3d(0, 0, 0)
            Dim pnt2 As New Point3d(1000, 1000, 0)
            Dim scale As Double = 10
            Dim vector As New Vector3d(0, 0, 0)
            Dim transform As Matrix3d = (Matrix3d.Scaling(scale, pnt2) _
                                         * Matrix3d.Displacement(pnt1.GetVectorTo(pnt2)))
            Dim idColl As New ObjectIdCollection
            Dim lastEnt As ObjectId = Utils.EntLast
            Dim newColor As Color = Color.FromColorIndex(ColorMethod.ByAci, 1)
            db.Insert(transform, tmpDb, True)
            Using tr As Transaction = doc.TransactionManager.StartTransaction
                Try
                    Dim acEntity As Entity
                    If (lastEnt = ObjectId.Null) Then
                        lastEnt = Utils.EntFirst
                        acEntity = DirectCast(tr.GetObject( _
                                lastEnt, OpenMode.ForWrite), Entity)
                        acEntity.Color = newColor
                    End If

                    Dim nextent As ObjectId = Utils.EntNext(lastEnt)
                    Do While (nextent <> ObjectId.Null)
                        acEntity = DirectCast(tr.GetObject( _
                                nextent, OpenMode.ForWrite), Entity)
                        acEntity.Color = newColor
                        nextent = Utils.EntNext(nextent)
                    Loop
                Catch exx As Autodesk.AutoCAD.Runtime.Exception
                    ed.WriteMessage((ChrW(10) & exx.ToString))
                End Try
                tr.Commit()
            End Using
        End Sub

--- End code ---


--- Code: ---
       <CommandMethod("InsDXF9")> _
        Public Sub InsDXF9()
            Dim doc As Document = Application.DocumentManager.MdiActiveDocument
            Dim ed As Editor = doc.Editor
            Dim db As Database = doc.Database
            Dim tmpDb As New Database(False, True)
            tmpDb.DxfIn("K:\\ToTest\\Squircle.dxf", "K:\\ToTest\Squircle.log")
            Dim pnt1 As New Point3d(0, 0, 0)
            Dim pnt2 As New Point3d(1000, 1000, 0)
            Dim scale As Double = 10
            Dim vector As New Vector3d(0, 0, 0)
            Dim transform As Matrix3d = (Matrix3d.Scaling(scale, pnt2) _
                                         * Matrix3d.Displacement(pnt1.GetVectorTo(pnt2)))
            Dim newColor As Color = Color.FromColorIndex(ColorMethod.ByAci, 1)
            Dim tr As Transaction = tmpDb.TransactionManager.StartTransaction
            Try
                Dim tmpDbBlockTable As BlockTable = DirectCast(tr.GetObject( _
                        tmpDb.BlockTableId, OpenMode.ForRead), BlockTable)
                Dim tmpDbModelSpace As BlockTableRecord = DirectCast(tr.GetObject( _
                        tmpDbBlockTable.Item(BlockTableRecord.ModelSpace), _
                        OpenMode.ForWrite), BlockTableRecord)

                Dim id As ObjectId
                For Each id In tmpDbModelSpace
                    Dim acEntity As Entity = DirectCast(tr.GetObject( _
                            id, OpenMode.ForWrite), Entity)
                    acEntity.Color = newColor
                Next

                tr.Commit()
            Catch exx As Autodesk.AutoCAD.Runtime.Exception
                ed.WriteMessage((ChrW(10) & exx.ToString))
            Finally
                If (Not tr Is Nothing) Then
                    tr.Dispose()
                End If
            End Try
            db.Insert(transform, tmpDb, True)
        End Sub

--- End code ---


kaefer:

--- Quote from: Kerry on March 29, 2011, 02:35:49 am ---
--- Code: ---Option Explicit On
Option Strict On

--- End code ---

--- End quote ---

Does the culprit sit right here?  

Cheers, Thorsten (missing bargepole, won't do VB)

Kerry:

--- Quote from: kaefer on March 29, 2011, 03:18:43 am ---
--- Quote from: Kerry on March 29, 2011, 02:35:49 am ---
--- Code: ---Option Explicit On
Option Strict On

--- End code ---

--- End quote ---

Does the culprit sit right here?  

Cheers, Thorsten (missing bargepole, won't do VB)

--- End quote ---

Don't know Thorsten, but it didn't seen correct to me to code with them off .... but that's probably just me :)

I've decided I need to at least be able to read VB and write a little ... as much as I hate the Dim word  :-P
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!

已领礼包: 1632个

财富等级: 堆金积玉

发表于 2014-5-10 19:21:47 | 显示全部楼层
以下是原贴中的VB代码


  1.         Public Sub InsDXF7()
  2.             Dim doc As Document = Application.DocumentManager.MdiActiveDocument
  3.             Dim ed As Editor = doc.Editor
  4.             Dim db As Database = doc.Database
  5.             Dim tmpDb As New Database(False, True)
  6.             tmpDb.DxfIn("K:\\ToTest\\Squircle.dxf", "K:\ToTest\\Squircle.log")
  7.             Dim pnt1 As New Point3d(0, 0, 0)
  8.             Dim pnt2 As New Point3d(1000, 1000, 0)
  9.             Dim scale As Double = 10
  10.             Dim vector As New Vector3d(0, 0, 0)
  11.             Dim transform As Matrix3d = (Matrix3d.Scaling(scale, pnt2) _
  12.                                          * Matrix3d.Displacement(pnt1.GetVectorTo(pnt2)))
  13.             Dim idColl As New ObjectIdCollection
  14.             Dim lastEnt As ObjectId = Utils.EntLast
  15.             Dim newColor As Color = Color.FromColorIndex(ColorMethod.ByAci, 1)
  16.             db.Insert(transform, tmpDb, True)
  17.             Using tr As Transaction = doc.TransactionManager.StartTransaction
  18.                 Try
  19.                     If (lastEnt = ObjectId.Null) Then
  20.                         lastEnt = Utils.EntFirst
  21.                         idColl.Add(lastEnt)
  22.                     End If

  23.                     Dim nextent As ObjectId = Utils.EntNext(lastEnt)
  24.                     Do While (nextent <> ObjectId.Null)
  25.                         idColl.Add(nextent)
  26.                         nextent = Utils.EntNext(nextent)
  27.                     Loop

  28.                     Dim id As ObjectId
  29.                     For Each id In idColl
  30.                         Dim acEntity As Entity = DirectCast(tr.GetObject( _
  31.                                 id, OpenMode.ForWrite), Entity)
  32.                         acEntity.Color = newColor
  33.                     Next

  34.                 Catch exx As Autodesk.AutoCAD.Runtime.Exception
  35.                     ed.WriteMessage((ChrW(10) & exx.ToString))
  36.                 End Try
  37.                 tr.Commit()
  38.             End Using
  39.         End Sub



论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 1 反对 0

使用道具 举报

已领礼包: 1632个

财富等级: 堆金积玉

发表于 2014-5-10 19:18:33 | 显示全部楼层
将楼上原贴其中一个例子的VB代码转成了C#代码, 现贴出来以便大家学习.

  1. public void InsDXF7()
  2. {
  3.   Document doc = AcadApp.DocumentManager.MdiActiveDocument;
  4.   Editor ed = doc.Editor; Database db = doc.Database;
  5.   Database tmpDb = new Database(false, true);
  6.   tmpDb.DxfIn("K:\\ToTest\\Squircle.dxf", "K:\\ToTest\\Squircle.log");
  7.   Point3d pnt1 = new Point3d(0, 0, 0);
  8.   Point3d pnt2 = new Point3d(1000, 1000, 0);
  9.   double scale = 10.0;
  10.   Vector3d vector = new Vector3d(0, 0, 0);
  11.   Matrix3d transform = Matrix3d.Scaling(scale, pnt2) * Matrix3d.Displacement(pnt1.GetVectorTo(pnt2));
  12.   ObjectId lastEnt = AcUtils.EntLast();
  13.   Autodesk.AutoCAD.Colors.Color newColor = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByAci, 1);
  14.   db.Insert(transform, tmpDb, true);
  15.   using (Transaction tr = doc.TransactionManager.StartTransaction())
  16.    {  
  17.            try
  18.     {   
  19.       if (lastEnt == ObjectId.Null)
  20.       {   
  21.         lastEnt = AcUtils.EntFirst();   
  22.         idColl.Add(lastEnt);   
  23.       }
  24.       ObjectId nextent = AcUtils.EntNext(lastEnt);   
  25.       while (nextent != ObjectId.Null)
  26.       {   
  27.         idColl.Add(nextent);   
  28.         nextent = AcUtils.EntNext(nextent);   
  29.       }
  30.       foreach (ObjectId id in idColl)
  31.       {   
  32.         Entity acEntity = (Entity)tr.GetObject(id, OpenMode.ForWrite);   
  33.         acEntity.Color = newColor;   
  34.       }  
  35.     }
  36.     catch (Autodesk.AutoCAD.Runtime.Exception exx)
  37.     {   
  38.       ed.WriteMessage(Constants.vbLf + exx.ToString());  
  39.     }  
  40.     tr.Commit();
  41.   }
  42. }


点评

赞一个,省得我再了解VB.NET了  详情 回复 发表于 2014-5-10 19:40
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

已领礼包: 859个

财富等级: 财运亨通

 楼主| 发表于 2014-5-10 19:40:13 来自手机 | 显示全部楼层
ScmTools 发表于 2014-5-10 19:18
将楼上原贴其中一个例子的VB代码转成了C#代码, 现贴出来以便大家学习.

赞一个,省得我再了解VB.NET了
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-12-19 00:22 , Processed in 0.509880 second(s), 38 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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