ndtnv 481 Báo cáo bài đăng Đã đăng Tháng 11 12, 2008 Khi cài các phiên bản khác nhau của acad và excel thì nó chạy không chính xác hay không chạy được Khắc phục là dùng object để hỗ trợ các ver khác nhau của cad và excel Dưới đây là ví dụ vẽ line dùng object từ (0,0,0) đến (1,1,0) lên ModelSpace của bản vẽ đang active Sub Example_AddLine() Dim acad As Object, anObj As Object Dim Pt1#(0 To 2), Pt2#(0 To 2) Set acad = GetObject(, "AutoCAD.Application") If Err <> 0 Then Set acad = CreateObject("AutoCAD.Application") If Err <> 0 Then Exit Sub End If End If acad.Visible = True Pt1(0) = 0#: Pt1(1) = 0#: Pt1(2) = 0# Pt2(0) = 1#: Pt2(1) = 1#: Pt2(2) = 0# Set anObj = acad.ActiveDocument.ModelSpace.AddLine(Pt1, Pt2) anObj.Update End Sub 1 Chia sẻ bài đăng này Liên kết tới bài đăng Chia sẻ trên các trang web khác
phantuhuong 227 Báo cáo bài đăng Đã đăng Tháng 11 12, 2008 Một việc rất quan trọng trước khi thoát khỏi thủ tục phải giải phóng biến: Set acad = Nothing Nếu không máy sẽ ì ạch và chẳng mấy chốc là treo máy. Chia sẻ bài đăng này Liên kết tới bài đăng Chia sẻ trên các trang web khác
ndtnv 481 Báo cáo bài đăng Đã đăng Tháng 11 13, 2008 Cảm ơn bạn Hướng đã góp ý. Đúng như vậy, vì đây là VD nên tôi ghép nhiều hàm trên máy tôi vào đây. Trong khi lập trình thì đầu tiên là phải tạo object Autocad chứ không phải là cứ mỗi line tạo 1 lần. Biến acad có là biến global hoặc là biến local của hàm main rồi giao tham biến cho hàm vẽ Ngoài ra tôi còn dùng các biến global cho những object hay dùng như acad.ActiveDocument.ModelSpace Sau khi hoàn thành các lệnh xong thì phải giải phóng tất cả các biến global. Trong VD trên thì acad là biến local nên khi ra khỏi hàm thì tự nó cũng giải phóng. Nhưng việc giải phóng biến là 1 thói quen tốt khi lập trình. Theo cách trên thì ta có thể vẽ mọi đối tượng từ excel ra acad. Các bạn tham khảo trong help của acad phần ActiveX and VBA Refence -> Code Example và đổi ThisDrawing.ModelSpace thành acad.ActiveDocument.ModelSpace Sub Example_AddLine() ' This example adds a line in model space Dim lineObj As AcadLine Dim startPoint(0 To 2) As Double Dim endPoint(0 To 2) As Double ' Define the start and end points for the line startPoint(0) = 1#: startPoint(1) = 1#: startPoint(2) = 0# endPoint(0) = 5#: endPoint(1) = 5#: endPoint(2) = 0# ' Create the line in model space Set lineObj = ThisDrawing.ModelSpace.AddLine(startPoint, endPoint) ZoomAll End Sub Từ đó thì nếu tổ chức data đầy đủ ở excel và các hàm vẽ cơ bản, ta chỉ cần 1 click là có thể xuất thẳng ra acad mà khỏi qua trung gian là file txt hay csv Chia sẻ bài đăng này Liên kết tới bài đăng Chia sẻ trên các trang web khác
phantuhuong 227 Báo cáo bài đăng Đã đăng Tháng 11 18, 2008 Bạn xem thêm ở đây để hiểu rõ hơn mối quan hệ này: http://www.cadviet.com/forum/index.php?showtopic=4290 Chia sẻ bài đăng này Liên kết tới bài đăng Chia sẻ trên các trang web khác
ndtnv 481 Báo cáo bài đăng Đã đăng Tháng 11 24, 2008 Nếu xem các VD của VBA của acad ta sẽ thấy hầu hết đều không có dòng Set Object = Nothing và nếu có cũng là chỉ thể hiện MsgBox là đã released object. Dưới đây là 2 VD trong ActiveX and VBA Reference – Code Example Sub Example_Selection() Dim ACADPref As AcadPreferencesSelection . . . ' Release Selection Preferences object Set ACADPref = Nothing MsgBox "We have now released the Selection Preferences object!" End Sub Sub Example_SelectionSets() Dim SSetColl As AcadSelectionSets Set SSetColl = ThisDrawing.SelectionSets ' Create a SelectionSet named "TEST" in the current drawing Dim ssetObj As AcadSelectionSet Set ssetObj = SSetColl.Add("TEST") MsgBox "A new SelectionSet called " & ssetObj.name & " has been added to the SelectionSets collection.", vbInformation, "SelectionSets Example" End Sub Chia sẻ bài đăng này Liên kết tới bài đăng Chia sẻ trên các trang web khác
ndtnv 481 Báo cáo bài đăng Đã đăng Tháng 11 24, 2008 Dưới đây là help của VB Object Data TypeObject variables are stored as 32-bit (4-byte) addresses that refer to objects. Using the Set statement, a variable declared as an Object can have any object reference assigned to it. Note Although a variable declared with Object type is flexible enough to contain a reference to any object, binding to the object referenced by that variable is always late (run-time binding). To force early binding (compile-time binding), assign the object reference to a variable declared with a specific class name. Set Statement Assigns an object reference to a variable or property. Syntax Set objectvar = {[New] objectexpression | Nothing} The Set statement syntax has these parts: Part Description objectvar Required. Name of the variable or property; follows standard variable naming conventions. New Optional. New is usually used during declaration to enable implicit object creation. When New is used with Set, it creates a new instance of the class. If objectvar contained a reference to an object, that reference is released when the new one is assigned. The New keyword can't be used to create new instances of any intrinsic data type and can't be used to create dependent objects. objectexpression Required. Expression consisting of the name of an object, another declared variable of the same object type, or a function or method that returns an object of the same object type. Nothing Optional. Discontinues association of objectvar with any specific object. Assigning Nothing to objectvar releases all the system and memory resources associated with the previously referenced object when no other variable refers to it. Remarks To be valid, objectvar must be an object type consistent with the object being assigned to it. The Dim, Private, Public, ReDim, and Static statements only declare a variable that refers to an object. No actual object is referred to until you use the Set statement to assign a specific object. . . . Generally, when you use Set to assign an object reference to a variable, no copy of the object is created for that variable. Instead, a reference to the object is created. More than one object variable can refer to the same object. Because such variables are references to the object rather than copies of the object, any change in the object is reflected in all variables that refer to it. However, when you use the New keyword in the Set statement, you are actually creating an instance of the object. Và đây là Example Set Statement ExampleThis example uses the Set statement to assign object references to variables. YourObject is assumed to be a valid object with a Text property. Dim YourObject, MyObject, MyStr Set MyObject = YourObject ' Assign object reference. ' MyObject and YourObject refer to the same object. YourObject.Text = "Hello World" ' Initialize property. MyStr = MyObject.Text ' Returns "Hello World". ' Discontinue association. MyObject no longer refers to YourObject. Set MyObject = Nothing ' Release the object. Như vậy việc Set MyObject = Nothing là ngắt liên kết của MyObject với đối tượng mà nó tham chiếu. Dòng trên có thể chỉ cần thiết khi tiếp theo còn nhiều đoạn code khác nữa. Còn nếu biến MyObject dùng đến cuối hàm thì vì nó là 1 biến local nên khi ra khỏi hàm thì VB sẽ tự động ngắt liên kết Chia sẻ bài đăng này Liên kết tới bài đăng Chia sẻ trên các trang web khác
ndtnv 481 Báo cáo bài đăng Đã đăng Tháng 11 24, 2008 Dưới đây là các hàm tôi đã dùng để kiểm tra Function GetAcad(bSet As Boolean) Dim obj As Object Set obj = GetObject(, "AutoCAD.Application") If Err <> 0 Then GetAcad = 0 Else GetAcad = 1 If bSet Then Set obj = Nothing End Function Sub TestAcad() Dim i As Long i = 0 While i < 200000 i = i + GetAcad(False) Wend End Sub Mở Windows Task Manager, gọi hàm GetAcad 200.000 lần với bSet là True hay False thì không có gì khác biệt về bộ nhớ của hệ thống Vì kiểu object chỉ chiếm 4 byte (hàm set cũng có thể lấy các tài nguyên khác của hệ thống) nên có thể sau khi gọi 200000 lần có thể không thể hiện sự khác biệt. Tuy nhiên hàm GetObject(, "AutoCAD.Application") tốn thời gian , vì vậy tôi đã dùng hàm lấy object là 1 cell của excel như sau Function GetCell(r&, c&) Dim obj As Object Set obj = Cells(r, c) If Err <> 0 Then GetCell = 0 Else GetCell = 1 End Function Sub TestCell() Dim i&, r&, c& i = 0: r = 1: c = 1 While i < 50000000 i = i + GetCell(r, c) c = c + 1 If c > 256 Then c = 1: r = r + 1 If r > 65536 Then r = 1 End If Wend End Sub Sau khi gọi 50 triệu lần thì bộ nhớ và tốc độ của hệ thống hầu như không đổi. Điều này chứng tỏ là VB đã " Release the object " với biến local và các tài nguyên khác nếu có của hệ thống sau khi ra khỏi hàm Chia sẻ bài đăng này Liên kết tới bài đăng Chia sẻ trên các trang web khác