boolVal = ODRcs.Init(gEntity, True, False) Error!!

boolVal = ODRcs.Init(gEntity, True, False) Error!!

mirwing76
Contributor Contributor
1,499 Views
9 Replies
Message 1 of 10

boolVal = ODRcs.Init(gEntity, True, False) Error!!

mirwing76
Contributor
Contributor

argument Error!! Function Init(acadObj As LPDISPATCH, openMode As Boolean, skipNested As Boolean) As Boolean

Member of AutocadMAP.ODRecords Initialize ODRecords with a new AutoCAD object identifier.

 

what is "LPDISPATCH" ? object Type? acadEntity ?

 

problem openMode OR skipNested ?

 

Cadmap ver.2017 , vba 7.?

0 Likes
1,500 Views
9 Replies
Replies (9)
Message 2 of 10

mirwing76
Contributor
Contributor

cad map 2017 64bit.

 

gEntity = getEntity

 

set obj = gEntity

 

....

....

....

 

 

 

ODRc.AttachTo (obj.ObjectID)

overflow !! error!!

 

debug.print obj.ObjectID

 1738998788544 

 

Function AttachTo(acadId As Long) As Boolean

Member of AutocadMAP.ODRecord

Returns the AutoCAD object id associated with the record.

 

0 Likes
Message 3 of 10

norman.yuan
Mentor
Mentor

I am not sure what exactly you want to do, but assume this is what you want to achieve:

 

1. Create a ODTable, either via code, or manually create one. So, the code works against a ODTable with known table name

2. For every entity in modelspace, you want to see if the entity has the OD record from that table already attached or not. If not, you want to attach the OD record from that table; if already attached, optinally, you may want to update the record's values.

 

Checking if an entity has a OD record with given table namke is where you need to call ODRecords.Init(), which retrieves all OD records attached to an entities (these records could be froom multiple ODTables).

 

Here is sample code that runs OK in my AutoCAD Map/Civil2017:

 

Option Explicit

Public Sub TestODTable()
    
    Dim proj As AutocadMAP.Project
    Dim table As AutocadMAP.ODTable
    Dim acMap As AutocadMAP.AcadMap
    
    Set acMap = ThisDrawing.Application.GetInterfaceObject("AutoCADMap.Application")
    Set proj = acMap.Projects(0)
    
    ''MsgBox proj.ODTables.Count & " Table(s) found"
    Set table = GetTargetTable("MY_OD_TABLE", proj)
    If table Is Nothing Then
        MsgBox "Object Data Table ""MY_OD_TABLE"" does not exist!"
        Exit Sub
    Else
        MsgBox "Object Data Table ""MY_OD_TABLE"" found"
    End If
    
    '' attach od record to all entities in modelspace with default values
    Dim ent As AcadEntity
    For Each ent In ThisDrawing.ModelSpace
        AttachOdRecord ent, table
    Next
      
End Sub

Private Function GetTargetTable( _
    tblName As String, proj As AutocadMAP.Project) As AutocadMAP.ODTable
    
    Dim tbl As AutocadMAP.ODTable
    For Each tbl In proj.ODTables
        If UCase(tbl.Name) = UCase(tblName) Then
            Set GetTargetTable = tbl
            Exit Function
        End If
    Next
    Set GetTargetTable = Nothing
    
End Function

Private Sub AttachOdRecord(ent As AcadEntity, tbl As AutocadMAP.ODTable)

    Dim r As AutocadMAP.ODRecord
    If Not HasTableRecord(ent, tbl) Then
        Set r = tbl.CreateRecord()
        r.AttachTo CLng(ent.ObjectID)
    End If
    
End Sub

Private Function HasTableRecord(ent As AcadEntity, tbl As AutocadMAP.ODTable) As Boolean
    
    Dim found As Boolean
    
    Dim r As AutocadMAP.ODRecord
    Dim rs As AutocadMAP.ODRecords
    Set rs = tbl.GetODRecords()
    If rs.Init(ent, False, True) Then
        
        Do Until rs.IsDone
            
            Set r = rs.Record
            If UCase(r.tableName) = UCase(tbl.Name) Then
                found = True
                Exit Do
            End If
            rs.Next
        Loop
    End If
    
    HasTableRecord = found

End Function

As you can see the code first verify the given table exists; then loop through each entity in modelspace to check if a record from that table is attached or not. If not, attach a record from that table. Pay attention to the function HasTableRecord(): This is where you are struggling. I think.

 

HTH

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 4 of 10

mirwing76
Contributor
Contributor
Thanks a lot for your help.

The code you created will work fine. In "autocad map 2004"
 It does not work with "autocadmap 2017 64bit".

"If rs.Init (ent, False, True) Then"
An error occurs here.

What did you load References from in vba?
In my case.
1. Visual Basic For Applications.
2. AutoCad 2017 Type Libray.
3. OLE Automation
4. Autodesk Map 3D 2005

I am having trouble upgrading from "map 2004" to "map 2017" because the existing code is not working properly.

Translated into "Google Translator".

 

0 Likes
Message 5 of 10

norman.yuan
Mentor
Mentor

I am confused. IN your original post, you mentioned it is AutoCAD Map 2017. Now you say AutoCAD Map 2004.

 

The code I showed runs OK with my AutoCAD Map 2017. 

 

Do you want it to run in AutoCAD Map 2017, or AutoCAD Map 2004? Or do you want the code run in both?

 

Since AutoCAD Map 2005 is the last version of AutoCAD Map's COM API ( reference to AcMapVbApi.tlb"), thus, for all version of AutoCAD Map since then, use the same COM API type library (AutoCAD Map 2005) for Map programming. If you use AutoCAD Map 2004, you still set reference to AcMapVbApi.tlb, which is different version (earlier version) that comes with AutoCAD Map 2004.

 

You cannot load the same *.dvb file in both AutoCAD Map 2004 and AutoCAD Map 2017. You need different version/file of the *.dvb, against corresponding version of AutoCAD Map, even though the code itself could be the same.

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 6 of 10

mirwing76
Contributor
Contributor

It should work correctly with "autocad map 2017 64bit".

 

You still get an error in the code you provided.

"If rs.Init (ent, False, True) Then"
The "rs.init" part.

 

Should I reinstall "autocad map"? Or should I reinstall "AutoCAD_2017_VBA_module_Win_64bit_dlm"?

 

I am always thankful for your help.

 

Translated into "Google Translator".

0 Likes
Message 7 of 10

flowerpapa
Enthusiast
Enthusiast

Hello Norman, 
could please tell us about which are reference file have to be linked with vba, 
because i have linked below references but still i get err in Map3d2019 version
Capture.PNG

0 Likes
Message 8 of 10

norman.yuan
Mentor
Mentor

I do not have AutoCAD Map/Civil 2019 available, but this code work OK both in AutoCAD Map 2018/2020:

 

Public Sub MapTest()

    Dim mapApp As AutocadMAP.AcadMap
    Set mapApp = ThisDrawing.Application.GetInterfaceObject("AutoCADMap.Application")
    MsgBox "Map app: " & mapApp.Projects.Count
    
End Sub

So, I do not see any reason why it does not work in AutoCAD Map 2019.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 9 of 10

flowerpapa
Enthusiast
Enthusiast

Actually I'm using civil3d2019 version

0 Likes
Message 10 of 10

norman.yuan
Mentor
Mentor

Well, I should have said that what I used to run the code were actually Civil2018/2020. So, again, I do not see why Civil2019 would make it differently.

Norman Yuan

Drive CAD With Code

EESignature

0 Likes