ObjectID from Excel to AutoCAD

ObjectID from Excel to AutoCAD

conveyor1
Enthusiast Enthusiast
2,151 Views
8 Replies
Message 1 of 9

ObjectID from Excel to AutoCAD

conveyor1
Enthusiast
Enthusiast

I have created some code to extract the information from an AutoCAD block and put it into a Excel Sheet.  I am now working on the code to put the updated information back into the drawing.

 

One of the items I extracted out was the ObjectID using the code: 

myXLWS.Cells(CurRow, 23) = myObjID.ToString

 

And it is stored in the excel sheet in this format:

(1943116255376)

 

However, when I try to use this value and try to find this block, I get this error:

 

Capture.PNG

 

Here is the code for the reverse process.

 

Dim myTransMan As DatabaseServices.TransactionManager
Dim myTrans As DatabaseServices.Transaction
Dim myDB As DatabaseServices.Database
Dim myXLApp As Microsoft.Office.Interop.Excel.Application
Dim myXLWS As Microsoft.Office.Interop.Excel.Worksheet
myXLApp = GetObject(, "Excel.Application")
myXLWS = myXLApp.ActiveSheet
myDB = ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database
myTransMan = myDB.TransactionManager
myTrans = myTransMan.StartTransaction
Dim CurRow As Long = 9
Dim LastRow As Long = myXLWS.UsedRange.Rows(myXLWS.UsedRange.Rows.Count).Row
If CurRow <> LastRow + 1 Then
   Dim myObjID As DatabaseServices.ObjectId = myXLWS.Cells(CurRow, 23)
   Dim myBlockRef As DatabaseServices.BlockReference =    myObjID.GetObject(DatabaseServices.OpenMode.ForRead)
   Class1_Obj.Setattributes(myObjID, "Length", myXLWS.Cells(CurRow, 2))
   CurRow = CurRow + 1
End If

' End Transactions

myTrans.Dispose()
myTransMan.Dispose()

0 Likes
2,152 Views
8 Replies
Replies (8)
Message 2 of 9

artc2
Autodesk
Autodesk
The value within an ObjectId is just a memory address, so it is only good for the current session of that drawing in AutoCAD. If you want something that is persistent in a drawing, then you should get the Handle from the ObjectId and store that in your spreadsheet.

For going the other way, you can use the Database.GetObjectId method to get the objectId from the handle.

If you really do want to work within the single drawing session using ObjectIds, then for going from the memory address you've stored in the spreadsheet, I believe you'll need to convert that to a System.IntPtr and then use the ObjectId constructor that takes a System.IntPtr argument to create an ObjectId that uses that memory address.
Message 3 of 9

dgorsman
Consultant
Consultant

Handles are not guaranteed unique between drawing files either, so it's generally a good idea to include a filename if not a full path to help ensure both data sets refer to the same thing.

 

Might also be a good idea to include an object type reference ("AcDbBlockReference", for example) before using the externally stored object data, in case someone overwrote the original DWG file with something else entirely.  That handle may refer to something else entirely in the new file and trying to treat a block reference as a line or text will cause problems.

----------------------------------
If you are going to fly by the seat of your pants, expect friction burns.
"I don't know" is the beginning of knowledge, not the end.


0 Likes
Message 4 of 9

conveyor1
Enthusiast
Enthusiast

Afternoon,

 

So no i have changed gear from looking it up with the ObjectID to the Block Handle.  I am able to now find the blocks by Handle from the excel sheet in the AutoCAD drawing.  However, the values from excel are coming in as System._ComObject.

 

Does anyone have any idea on how to correct this?

 

Line of code where it starts to error: 

If myBlockRef.Handle.Value = myXLWS.Cells(CurRow, 18) Then

0 Likes
Message 5 of 9

JamesMaeding
Advisor
Advisor

I think you have to do the .Value prop on cells method


internal protected virtual unsafe Human() : mostlyHarmless
I'm just here for the Shelties

0 Likes
Message 6 of 9

conveyor1
Enthusiast
Enthusiast

I eventually did a sub routine to fine the values.

 

Here is the code:

 

Main Program:

Class1_Obj.Setattributes(myObjID, "Length", GetExcelValue(myXLWS.Cells(CurRow, 2)))

 

Sub Routine:

 

Function GetExcelValue(ByVal ExcelCell As Excel.Range) As String
Dim EXLRANGE As Excel.Range
EXLRANGE = CType(ExcelCell, Excel.Range)
Dim ExcelValue As String
ExcelValue = EXLRANGE.Value
Return ExcelValue
End Function

0 Likes
Message 7 of 9

JamesMaeding
Advisor
Advisor

why not just:

Class1_Obj.Setattributes(myObjID, "Length", myXLWS.Cells(CurRow, 2).Value)

I think your sub just shuffles the same data around and typing .Value is easier than GetExcelValue( by 8 characters plus no sub to maintain. Take the easy ones 🙂


internal protected virtual unsafe Human() : mostlyHarmless
I'm just here for the Shelties

0 Likes
Message 8 of 9

conveyor1
Enthusiast
Enthusiast
Afternoon,

I did try that. But it was not an option.

Only options were equals, gethashcode, getype and tostring.
0 Likes
Message 9 of 9

JamesMaeding
Advisor
Advisor

that makes no sense, your GetExcelValue takes in an Excel.Range, then converts it to same type with CType.

Wonder why...

 

Function GetExcelValue(ByVal ExcelCell As Excel.Range) As String
Dim EXLRANGE As Excel.Range
EXLRANGE = CType(ExcelCell, Excel.Range)


internal protected virtual unsafe Human() : mostlyHarmless
I'm just here for the Shelties

0 Likes