1. 程式人生 > >教你如何用VB.NET編寫AutoCAD中的變色的溫度計

教你如何用VB.NET編寫AutoCAD中的變色的溫度計

這個例子我們去年在DevDays培訓中介紹AutoCAD 2010 API的時候演示過,現在我把關鍵的程式碼貼上來。AutoCAD.NET API不支援自定義實體,但是有個叫overrule的技術,對於想用.net來實現自定義實體的使用者來說,這個例子是個入門教程。

#Region "HelperClass"

'Global helper class (singleton). Contains central definitions of some global constants, and a few helper functions

Public Class HelperClass

Const

mExtDictName As String = "SGP_MyDict" 'Defines Dictionary name for the Extension Dictionary demo

Const mXRecName As String = "SGP_MyDATA" 'Defines Dictionary name for the Extension Dictionary demo

Private Shared mMe As HelperClass

'Name of our dictionary in extension dictionary

Public ReadOnly

Property DictionaryName()

Get

Return mExtDictName

End Get

End Property

'Name of our XRecord

Public ReadOnly Property XRecordName()

Get

Return mXRecName

End Get

End Property

'Protected constructor - to enforce singleton behavior

Protected Sub New()

End Sub

'static function to retrieve one and only instance of singleton

Shared ReadOnly Property GetSingleton()

Get

If mMe Is Nothing Then

mMe = New HelperClass

End If

Return mMe

End Get

End Property

'Retrieve data (as resbuf) from or Xrecord.

'Returns null object if there's a problem

Public Function GetXRecordData(ByVal obj As DBObject) As ResultBuffer

Dim xRec As Xrecord = Nothing

Dim id As ObjectId = obj.ExtensionDictionary

'Make sure we have an ext dict befoore proceeding

If id.IsValid Then

'Retrieve data using a transaction

Dim db As Database = Application.DocumentManager.MdiActiveDocument.Database

Using tr As Transaction = db.TransactionManager.StartTransaction

Dim extDict As DBDictionary = tr.GetObject(id, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead, False)

If extDict.Contains(DictionaryName) Then

'We're assuming that if my dictionary exists, then so will the XRecord in it.

Dim dictId As ObjectId = extDict.GetAt(DictionaryName)

Dim myDict As DBDictionary = tr.GetObject(dictId, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead, False)

xRec = tr.GetObject(myDict.GetAt(XRecordName), Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead, False)

End If

End Using

End If

If xRec Is Nothing Then

Return Nothing

Else

Return xRec.Data

End If

End Function

'Modifies data in our XRecord.

'(creates ou rdictionary and XRecoird if it doesn't already exist)

Public Sub SetXRecordData(ByVal obj As DBObject, ByVal myData As ResultBuffer)

Dim db As Database = Application.DocumentManager.MdiActiveDocument.Database

Using tr As Transaction = db.TransactionManager.StartTransaction

Dim myDict As DBDictionary

Dim xRec As Xrecord = Nothing

Dim id As ObjectId = obj.ExtensionDictionary

If id = ObjectId.Null Then

obj.CreateExtensionDictionary()

id = obj.ExtensionDictionary

End If

Dim extDict As DBDictionary = tr.GetObject(id, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite, False)

If extDict.Contains(DictionaryName) Then

Dim dictId As ObjectId = extDict.GetAt(DictionaryName)

myDict = tr.GetObject(dictId, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite, False)

Else

myDict = New DBDictionary

extDict.SetAt(DictionaryName, myDict)

tr.AddNewlyCreatedDBObject(myDict, True)

End If

If myDict.Contains(XRecordName) Then

xRec = tr.GetObject(myDict.GetAt(XRecordName), Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite, False)

Else

xRec = New Xrecord

myDict.SetAt(XRecordName, xRec)

tr.AddNewlyCreatedDBObject(xRec, True)

End If

xRec.Data = myData

tr.Commit()

End Using

End Sub

End Class

#End Region

#Region "Simple Grip Overrule"

'Grip overrule to add our custom grips to the line

Public Class MyGripOverrule

Inherits GripOverrule

'Our custom grip class

'(Could have derived one class for each grip, but we'll use member dara (Ordinal property) to distinguis grips instead)

Public Class MyGrip

Inherits GripData

Private mGripNum As Integer

Public Property Ordinal() As Integer

Get

Return mGripNum

End Get

Set(ByVal value As Integer)

mGripNum = value

End Set

End Property

'Call this to tell the grip to move itself

Public Sub Move(ByVal vec As Vector3d)

GripPoint = GripPoint + vec

End Sub

'Grip draws itself

Public Overrides Function ViewportDraw(ByVal worldDraw As Autodesk.AutoCAD.GraphicsInterface.ViewportDraw, ByVal entityId As Autodesk.AutoCAD.DatabaseServices.ObjectId, ByVal type As Autodesk.AutoCAD.DatabaseServices.GripData.DrawType, ByVal imageGripPoint As Autodesk.AutoCAD.Geometry.Point3d?, ByVal gripSizeInPixels As Integer) As Boolean

Dim unit As Point2d = worldDraw.Viewport.GetNumPixelsInUnitSquare(GripPoint)

worldDraw.Geometry.Circle(GripPoint, 1.5 * gripSizeInPixels / unit.X, worldDraw.Viewport.ViewDirection)

Return True

End Function

End Class

'Array to hold our 3 grips

Dim mGripData(2) As GripData

Public Overrides Sub GetGripPoints(ByVal entity As Autodesk.AutoCAD.DatabaseServices.Entity, ByVal grips As Autodesk.AutoCAD.DatabaseServices.GripDataCollection, ByVal curViewUnitSize As Double, ByVal gripSize As Integer, ByVal curViewDir As Autodesk.AutoCAD.Geometry.Vector3d, ByVal bitFlags As Autodesk.AutoCAD.DatabaseServices.GetGripPointsFlags)

Dim rb As ResultBuffer = HelperClass.GetSingleton.GetXRecordData(entity)

'We assume entity is a line

Dim myLine As Line = entity

'Set grip positions to represent temperatures (we're using Celsius)

'min temperature

Dim temp As Integer = rb.AsArray(1).Value

Dim pos As Double = myLine.StartParam + (temp / 100) * (myLine.EndParam - myLine.StartParam)

Dim pt As Point3d = myLine.GetPointAtParameter(pos)

Dim grip As New MyGrip

grip.Ordinal = 0

grip.GripPoint = pt

mGripData(0) = grip

'max temperature

temp = rb.AsArray(2).Value

pos = myLine.StartParam + (temp / 100) * (myLine.EndParam - myLine.StartParam)

pt = myLine.GetPointAtParameter(pos)

grip = New MyGrip

grip.Ordinal = 1

grip.GripPoint = pt

mGripData(1) = grip

'current temperature

temp = rb.AsArray(3).Value

pos = myLine.StartParam + (temp / 100) * (myLine.EndParam - myLine.StartParam)

pt = myLine.GetPointAtParameter(pos)

grip = New MyGrip

grip.Ordinal = 2

grip.GripPoint = pt

mGripData(2) = grip

'Add our grips to the list

For Each g As MyGrip In mGripData

grips.Add(g)

Next

'Get the standard line grip points as well

MyBase.GetGripPoints(entity, grips, curViewUnitSize, gripSize, curViewDir, bitFlags)

End Sub

Public Overrides Sub MoveGripPointsAt(ByVal entity As Autodesk.AutoCAD.DatabaseServices.Entity, ByVal grips As Autodesk.AutoCAD.DatabaseServices.GripDataCollection, ByVal offset As Autodesk.AutoCAD.Geometry.Vector3d, ByVal bitFlags As Autodesk.AutoCAD.DatabaseServices.MoveGripPointsFlags)

'We only takeaction when we get this call on a database resident entity

'Dragging operation makes shallow clone of line, and setting clomeMeForDragging to false is generally a bad idea.

'(If you do set clone me for dragging to false, then don't call bae class overriden methods).

If entity.Id.IsValid Then

'Cast to a Line so we can access properties

Dim myLine As Line = entity

Dim lineDir As Vector3d = (myLine.EndPoint - myLine.StartPoint)

lineDir = lineDir.GetNormal 'Direction of Line

Dim offsetDist As Double = lineDir.DotProduct(offset) 'Component of mouse translation along like

'Iterate through list of all grips being moved

For Each g As GripData In grips

If TypeOf g Is MyGrip Then

Dim grip As MyGrip = g 'Cast to our grip type

'Make sure offset never takes grip beyond either end of line

If offsetDist >= 0 Then

If offsetDist > (myLine.EndPoint - grip.GripPoint).Length Then

offsetDist = (myLine.EndPoint - grip.GripPoint).Length

End If

Else

If -offsetDist > (myLine.StartPoint - grip.GripPoint).Length Then

offsetDist = -(myLine.StartPoint - grip.GripPoint).Length

End If

End If

lineDir = lineDir * offsetDist

'retrieve stored data and edit the changed value

Dim rb As ResultBuffer = HelperClass.GetSingleton.GetXRecordData(entity)

Dim val1 As String = rb.AsArray(0).Value

Dim intVal(2) As Integer

intVal(0) = rb.AsArray(1).Value 'min

intVal(1) = rb.AsArray(2).Value 'max

intVal(2) = rb.AsArray(3).Value 'current

'Tell grip to move itself long the line

grip.Move(lineDir)

'Calculate new temperature from grip position along the line

Dim newParam As Double = myLine.GetParameterAtPoint(grip.GripPoint)

Dim newTemp As Integer = 100 * (newParam - myLine.StartParam) / (myLine.EndParam - myLine.StartParam)

'Don't let min temp value rise above max temp

'And don't let max temp go below min temp

If grip.Ordinal = 0 Then

If newTemp < intVal(1) Then

intVal(0) = newTemp

Else

intVal(0) = intVal(1) - 1

End If

ElseIf grip.Ordinal = 1 Then

If newTemp > intVal(0) Then

intVal(1) = newTemp

Else

intVal(1) = intVal(0) + 1

End If

Else

intVal(2) = newTemp

End If

'Create new resbuf with new data and put back in Xrecord

Dim newRb As ResultBuffer = New ResultBuffer(New TypedValue(DxfCode.Text, val1), _

New TypedValue(DxfCode.Int32, intVal(0)), _

New TypedValue(DxfCode.Int32, intVal(1)), _

New TypedValue(DxfCode.Int32, intVal(2)))

HelperClass.GetSingleton.SetXRecordData(myLine, newRb)

End If

Next

End If

'Remove our grips from the list befroe calling base class function

'(Doesn't seem to like my grips)

For i As Integer = grips.Count - 1 To 0 Step -1

If TypeOf grips(i) Is MyGrip Then

grips.Remove(grips(i))

End If

Next

相關推薦

如何用VB.NET編寫AutoCAD變色溫度計

這個例子我們去年在DevDays培訓中介紹AutoCAD 2010 API的時候演示過,現在我把關鍵的程式碼貼上來。AutoCAD.NET API不支援自定義實體,但是有個叫overrule的技術,對於想用.net來實現自定義實體的使用者來說,這個例子是個入門教程。 #Re

C語言編寫"vb"程式

  相信不少人學過C語言,也學過VB。在不少人看來C語言和VB之間最大的區別就是:C程式是黑漆漆DOS視窗,而VB是標準的Windows窗體。其實不然,C語言也是可以寫出“vb”程式的。 請看程式碼: #include <windows.h> LRESULT CA

【小家java】Java的執行緒池,真的對了嗎?(正確的姿勢使用執行緒池)

相關閱讀 【小家java】java5新特性(簡述十大新特性) 重要一躍 【小家java】java6新特性(簡述十大新特性) 雞肋升級 【小家java】java7新特性(簡述八大新特性) 不溫不火 【小家java】java8新特性(簡述十大新特性) 飽受讚譽 【小家java】java9

維基百科的資料科學:手把手Python讀懂全球最大百科全書

編譯:狗小白、李佳、張弛、魏子敏 沒人否認,維基百科是現代最令人驚歎的人類發明之一。 幾年前誰能想到,匿名貢獻者們的義務工作竟創造出前所未有的巨大線上知識庫?維基百科不僅是你寫大學論文時最好的資訊渠道,也是一個極其豐富的資料來源。 從自然語言處理到監督式機器學習,維

一步步讀懂NETIL(圖文詳解)

接觸NET也有1年左右的時間了,NET的內部實現對我產生了很大的吸引力。個人覺得:能對這些底部的實現進行了解和熟練的話,對以後自己寫程式碼是有很大幫助的,好了,廢話不多說,請看下邊: .NET CLR 和 Java VM 都是堆疊式虛擬機器器(Stack-Based VM),

手把手.NET Core寫爬蟲

寫在前面 自從上一個專案58HouseSearch從.NET遷移到.NET core之後,磕磕碰碰磨蹭了一個月才正式上線到新版本。 然後最近又開了個新坑,搞了個Dy2018Crawler用來爬dy2018電影天堂上面的電影資源。這裡也藉機簡單介紹一下如何基於

一步步讀懂NETIL(附帶圖)

    一步步教你讀懂NET中IL(附詳細圖)   接觸NET也有1年左右的時間了,NET的內部如何實現對我產生了很大的吸引力,在msdn上找到一篇關於NET的IL程式碼的圖解說明,寫的挺不錯的,在此基礎上加上個人的理解,每一個步驟都附帶圖解說明,如果你以前對NET中IL感覺晦澀難懂,頭昏腦漲的時候,沒關

Inkspace提取pdf的向量圖【超詳細】

我只想說Inkspace真是膩害,簡直太方便了!媽媽再也不用擔心我只能用ctrl+alt+a截渣渣圖了~ 1、開啟Inkspace 2、開啟一個pdf 可確保匯出的圖絕對高清 3、將畫布縮小到需要選取的圖片的範圍: 方法一: a、選擇第二個工具 用滑鼠畫出選框,只要確保

手把手jumpserver搭建堡壘機!

ict 添加用戶 以及 這一 用戶名 端口 cal cti tom 首先,jumpserver是什麽呢? Jumpserver 是一款由Python編寫開源的跳板機(堡壘機)系統,實現了跳板機應有的功能。基於ssh協議來管理,客戶端無需安裝agent。 特點: 完全開源,G

Fiddler在電腦上抓手機上的包

手機抓包 fiddler抓手機包 fiddler抓包 代理抓包 電腦上抓包很方便,並且很多種抓包工具。但是我們如果想要抓手機上的包並且分析它, 好像就比較麻煩了。但是我們用Fiddler可以解決這個問題。 首先我們在電腦上打開Fiddler並且設置,進入Tools---Fiddler Opti

古詩詞詮釋人工智能境界

人工智能境界現代社會,如果能用古詩詞來全是某種事物,那很多人都會稱贊你有文化,有底蘊。現在小編就帶你看看微軟大中華區首席戰略官彭壯壯是怎樣用古詩詞來詮釋人工智能境界的吧!第一重境界:一劍橫空星鬥寒,甫隨平北復征蠻。2015年6月,微軟的技術使圖象識別錯誤率降低到3.5%,而普通人類錯誤率是5.1%,這是一個非

手把手ngrx管理Angular狀態

cli emit spl 工作 準備就緒 優雅 spa 現在 改字體 本文將與你一起探討如何用不可變數據儲存的方式進行Angular應用的狀態管理 :ngrx/store——Angular的響應式Redux。本文將會完成一個小型簡單的Angular應用,最終代碼可以在這裏下

手把手EST進行固件降級

希捷固件降級範例型號: ST3600057SS 固件版本 EN03 為DELL EQL存儲設備用的硬盤固件,像這類使用非標準扇區字節數,又或者關閉硬盤寫入緩存的情況,是不能在普通PC上使用,或者表速度很慢,所以我們進行降級固件,讓其能在普通PC上正常使用。本文出自 “EST硬盤之家” 博客,請務必保留此出處

Kindle 推送教程:電子郵箱推送電子書

電子郵件 mail rec 實用 alt nal tom odi 批量 Kindle 推送是什麽意思?如何通過電子郵件附件推送?或許剛剛接觸 Kindle 的朋友對這個概念不是很清楚,其實所謂 Kindle 推送是指亞馬遜提供的一個“Kindle 個人文檔服務”,我們只需要

如何在word文檔輸入攝氏度符號

word文檔 攝氏度符號 下面有三種方法可以在word文檔輸入攝氏度符號,輸入攝氏度三種方法分別是:快捷鍵、輸入法、插入符號。 word攝氏度符號的輸入有很多種方法,一一介紹。 word攝氏度符號輸入方法一: 使用搜狗一類的輸入法,當我們輸入ssd,選擇2就是攝氏度℃了。 word

手把手npm發布一個包,詳細教程

文件夾 模塊 .com png keyword tor 速度慢 index bsp 我們已經實現了路由的自動化構建,但是我們可以看到,一大串代碼懟在裏面。當然你也可以說,把它封裝在一個JS文件裏面,然後使用require(‘./autoRoute.js‘)給引入進來,那也行

實戰分享,藍牙在小程序的應用

success 查詢 notify 用戶 等待 apt service() 傳遞數據 length 歡迎大家前往騰訊雲技術社區,獲取更多騰訊海量技術實踐幹貨哦~ 作者:朱勝 導語 藍牙在日常生活中廣泛使用的一項技術,小程序給了我們前端工程師一個控制藍牙的方法

Google工程師:樹莓派+Arduino+TensorFlow搭建圖像識別小車

href ble ogl dds .com ybds xls ddx wpa 6o呢鄰譚yq禿究乜km著樟派http://masbjyxls.wikidot.com/od潘礁弊x3苯悅詡lb厴勻瘓http://rhzwsbjsfz.wikidot.com/yc崗誦新oq儷簇

夠拼樂手機賺錢!

找到 介紹 發短信 自己 sans .cn src 用戶 二維 http://www.3agpl.com手機在現在已經普及到幾乎人手一部,每個人都“機不離手”。據統計:現在移動端上網已經超過了PC端,也就是手機端上網用戶已經超過了電腦端!由此可以證明,手機已經覆蓋大部分人群

手把手webpack3搭建react項目(開發環境和生產環境)(一)

stc reac config nod top llb cor git history 開發環境和生產環境整個配置源碼在github上,源碼地址:github-webpack-react 如果覺得有幫助,點個Star謝謝!! (一)是開發環境,(二)是生產環境。 一、首