找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 408|回复: 0

[ARX程序]:VB.net Arx

[复制链接]
发表于 2004-9-23 15:45:23 | 显示全部楼层 |阅读模式

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

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

×
Imports System
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.ApplicationServices
Imports System.Reflection
Imports System.IO
Imports System.Collections
Imports System.Runtime.InteropServices
Imports Autodesk.AutoCAD.Interop
Imports System.Diagnostics
Imports Autodesk.AutoCAD.PlottingServices
Imports DBTransMan = Autodesk.AutoCAD.DatabaseServices.TransactionManager

'to test:
'1. use the NETLOAD command to load mgdarx.dll
'2. issue one of the supported commands (XREC, HYPER, COMINTEROP)
Namespace MgdArx
    _

    Public Class MyCommands

        'this method will be automatically hooked into the AutoCAD command interpreter
        'type "cominterop" on the command line after loading this application with
        'the NETLOAD command
        <CommandMethod("COMINTEROP", CommandFlags.Session)> _
        Public Shared Sub ComInteropCommand()
            'use AcadApplication property to the to the COM object model
            Dim acadApp As AcadApplication = CType(Application.AcadApplication, AcadApplication)
            'use the COM object model to iterate through the docs
            Dim acadDoc As AcadDocument
            For Each acadDoc In acadApp.Documents
                'use the static FromAcadDocument method to convert a COM
                'document to a mnaged document
                Dim doc As Document = Document.FromAcadDocument(acadDoc)

                'Declare a document manager object
                Dim docMgr As DocumentManager = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager
                ' use the document manager to lock the document...note:  disposal of the lock (below) unlocks the document!
                Dim docLock As DocumentLock = docMgr.LockDocument(doc)

                Try
                    'use ObjectId to convert between COM wrappers for database
                    'objects and their managed wrappers.
                    Dim id As New ObjectId()
                    id.OldId = acadDoc.ActiveLayer.ObjectID
                    Dim ltr As LayerTableRecord = CType(id.Open(OpenMode.ForWrite), LayerTableRecord)
                    Try
                        ltr.Description = "Changed by the cominterop command"
                    Finally
                        ltr.Dispose()
                    End Try
                Finally
                    docLock.Dispose() ' unlock the document
                End Try
            Next acadDoc
        End Sub 'ComInteropCommand

        'this method will be automatically hooked into the AutoCAD command interpreter
        <CommandMethod("HYPER")> _
        Public Shared Sub HyperCommand()
            Dim db As Database = Application.DocumentManager.MdiActiveDocument.Database
            Dim tm As DBTransMan = db.TransactionManager
            'start a transaction
            Dim myT As Transaction = tm.StartTransaction()
            Try
                'create a line
                Dim line As New Line(New Point3d(0, 0, 0), New Point3d(1, 1, 0))
                Dim bt As BlockTable = CType(tm.GetObject(db.BlockTableId, OpenMode.ForRead, False), BlockTable)
                Dim btr As BlockTableRecord = CType(tm.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite, False), BlockTableRecord)
                'add it to the model space block table record
                btr.AppendEntity(line)
                'make sure that the transaction knows about this new object
                tm.AddNewlyCreatedDBObject(line, True)
                'add some hyperlinks
                Dim hyper As New HyperLink()
                hyper.Name = "www.autodesk.com"
                line.Hyperlinks.Add(hyper)
                If line.Hyperlinks.Contains(hyper) Then
                    hyper.Name = "www.gotdotnet.com"
                End If
                line.Hyperlinks.Add(hyper)
                Dim i As Integer
                For i = 0 To line.Hyperlinks.Count - 1
                    Debug.WriteLine(line.Hyperlinks(i).ToString())
                Next i 'commit transaction
                myT.Commit()
            Finally
                myT.Dispose()
            End Try
        End Sub 'HyperCommand

        <CommandMethod("XRECORD")> _
        Public Shared Sub XRecCommand()
            Dim db As Database = Application.DocumentManager.MdiActiveDocument.Database
            Dim tm As DBTransMan = db.TransactionManager
            'start a transaction
            Dim myT As Transaction = tm.StartTransaction()
            Try
                Dim rec As New Xrecord()
                rec.Data = New ResultBuffer( _
                        New TypedValue(CInt(DxfCode.Text), "This is a test"), _
                        New TypedValue(CInt(DxfCode.Int8), 0), _
                        New TypedValue(CInt(DxfCode.Int16), 1), _
                        New TypedValue(CInt(DxfCode.Int32), 2), _
                        New TypedValue(CInt(DxfCode.HardPointerId), db.BlockTableId), _
                        New TypedValue(CInt(DxfCode.BinaryChunk), New Byte() {0, 1, 2, 3, 4}), _
                        New TypedValue(CInt(DxfCode.ArbitraryHandle), db.BlockTableId.Handle), _
                        New TypedValue(CInt(DxfCode.UcsOrg), New Point3d(0, 0, 0)))

                Dim dict As DBDictionary = CType(myT.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForWrite, False), DBDictionary)
                dict.SetAt("test", rec)
                tm.AddNewlyCreatedDBObject(rec, True)
                'list the entries we just added
                CommandLinePrompts.Message("Xrecord items:" + ControlChars.Lf)
                Dim rb As TypedValue
                For Each rb In rec.Data
                    CommandLinePrompts.Message(String.Format("TypeCode={0}, Value={1}" + ControlChars.Lf, rb.TypeCode, rb.Value))
                Next rb
                'add some xdata on the xrecord (silly but this is just a test)
                'first have to register an app
                Dim tbl As RegAppTable = CType(myT.GetObject(db.RegAppTableId, OpenMode.ForWrite, False), RegAppTable)
                Dim app As New RegAppTableRecord()
                app.Name = "MyApp"
                tbl.Add(app)
                tm.AddNewlyCreatedDBObject(app, True)

                rec.XData = New ResultBuffer(New TypedValue(CInt(DxfCode.ExtendedDataRegAppName), "MyApp"), New TypedValue(CInt(DxfCode.ExtendedDataAsciiString), "This is some xdata string"))
                'list them
                CommandLinePrompts.Message("Xdata items:" + ControlChars.Lf)
                For Each rb In rec.XData
                    CommandLinePrompts.Message(String.Format("TypeCode={0}, Value={1}" + ControlChars.Lf, rb.TypeCode, rb.Value))
                Next rb
                'commit transaction
                myT.Commit()
            Finally
                myT.Dispose()
            End Try
        End Sub 'XRecCommand
    End Class 'MyCommands
End Namespace 'MgdArx
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-9-22 04:14 , Processed in 0.441618 second(s), 33 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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