Jump to content

Recommended Posts

Posted

Hi, can anyone help me, please? I need to "translate" this code in vba to autoit!

This is the VBA Code:

Sub Example_AddMtext()
    ' This example creates an MText object in model space.
    
    Dim MTextObj As AcadMText
    Dim corner(0 To 2) As Double
    Dim width As Double
    Dim text As String
    corner(0) = 0#: corner(1) = 10#: corner(2) = 0#
    width = 10
    text = "This is the text String for the mtext Object"

    ' Creates the mtext Object
    Set MTextObj = ThisDrawing.ModelSpace.AddMText(corner, width, text)
    ZoomAll
    
End Sub

I've Tried to do this:

$oAcad = ObjGet("","AutoCAD.Application.20")
If @error Then
    Msgbox(0,"ObjGet","Failed to open AutoCAD.Application.20 Object. Error code: " & hex(@error,8))
    Exit
Endif

_Add_MText($oAcad, 0, 10, "test")


Func _Add_MText($oAcad, $x, $y, $text, $width = 10)
   Local $aCoords[3] = [$x,$y,0]
   $oAcad.AcadMText.ThisDrawing.ModelSpace.AddMText($aCoords, $width, $text)
   Return 0
EndFunc

But i'm getting this error:

$oAcad.AcadMText.ThisDrawing.ModelSpace.AddMText($aCoords, $width, $text)
$oAcad^ ERROR

 

Can anyone give me a light, please?

Thanks a lot

Posted

What is the full error message you see in the SciTE output pane?

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted
  On 11/18/2016 at 5:31 PM, water said:

What is the full error message you see in the SciTE output pane?

Expand  

"C:\Users\admin\Desktop\autocadtest.au3" (13) : ==> The requested action with this object has failed.:
$oAcad.AcadMText($oAcad.ThisDrawing.ModelSpace.AddMText($aCoords, $width, $text))
$oAcad.AcadMText($oAcad^ ERROR
>Exit code: 1    Time: 0.5987

 

 

thanks for your response!

Posted

Maybe

$oAcad.Application.ActiveDocument.Modelspace.AddMTxt($aCoords, $width, $text)

or

$oAcad.ActiveDocument.Modelspace.AddMTxt($aCoords, $width, $text)

 

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted
  On 11/18/2016 at 5:50 PM, water said:

Maybe

$oAcad.Application.ActiveDocument.Modelspace.AddMTxt($aCoords, $width, $text)

or

$oAcad.ActiveDocument.Modelspace.AddMTxt($aCoords, $width, $text)

 

Expand  

Nothing =/

 

This example code works:

$strActiveDocument = $oAcad.Activedocument.Name
MsgBox(0,"$oAcad.Activedocument.Name",$strActiveDocument)

$iBlocks = $oAcad.Activedocument.Blocks.Count
MsgBox(0,"$oAcad.Activedocument.Blocks.Count",$iBlocks)
For $i = 0 to $iBlocks -1
    $strBlockName = $oAcad.Activedocument.Blocks.Item($i).name
    If $strBlockName = "TITLEBLOCK" Then
        $iAttributes = $oAcad.Activedocument.Blocks.Item($i).GetAttributes()
        MsgBox(0,"GetAttributes()",$iAttributes)
    EndIf
Next

But my code don't work =S

i've tried a lot of combination, but notting

Posted

add COM Error handler and try this:

$oAcad = ObjGet("","AutoCAD.Application.20")
If @error Then
    Msgbox(0,"ObjGet","Failed to open AutoCAD.Application.20 Object. Error code: " & hex(@error,8))
    Exit
Endif

Local $oTest1 = $oAcad.AcadMText
ConsoleWrite('Test1:' & IsObj($oTest1))
ConsoleWrite('Test1:' & VarGetType($oTest1))
ConsoleWrite('Test1:' & ObjName($oTest1))

Local $oTest2 = $oAcad.AcadMText.ThisDrawing
ConsoleWrite('Test2:' & IsObj($oTest2))
ConsoleWrite('Test2:' & VarGetType($oTest2))
ConsoleWrite('Test2:' & ObjName($oTest2))

Local $oTest3 = $oAcad.AcadMText.ThisDrawing.ModelSpace
ConsoleWrite('Test3:' & IsObj($oTest3))
ConsoleWrite('Test3:' & VarGetType($oTest3))
ConsoleWrite('Test3:' & ObjName($oTest3))

_Add_MText($oAcad, 0, 10, "test")


Func _Add_MText($oAcad, $x, $y, $text, $width = 10)
   Local $aCoords[3] = [$x,$y,0]
   $oAcad.AcadMText.ThisDrawing.ModelSpace.AddMText($aCoords, $width, $text)
   Return 0
EndFunc

REMARK: AutoIt code created with forum editor - not tested .....

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted

Testing one by one i'm getting the same error:

"C:\Users\rodol\Desktop\Novo(a) AutoIt v3 Script (3).au3" (8) : ==> The requested action with this object has failed.:
Local $oTest1 = $oAcad.AcadMText
Local $oTest1 = $oAcad^ ERROR

""C:\Users\rodol\Desktop\Novo(a) AutoIt v3 Script (3).au3" (10) : ==> The requested action with this object has failed.:
Local $oTest2 = $oAcad.AcadMText.ThisDrawing
Local $oTest2 = $oAcad^ ERROR"

 

 

Posted

The first object after the application object ($oAcad) has to be a document. At least that's how I understand the object model: http://www.afralisp.net/reference/autocad-object-model.php#

So could you please try:

$oAcad = ObjGet("","AutoCAD.Application.20")
$oDoc = $oAcad.ActiveDocument
$oMS = $oDoc.Modelspace
$vTxt = $oMS.AddMTxt($aCoords, $width, $text)

 

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted
  On 11/19/2016 at 7:37 AM, water said:

The first object after the application object ($oAcad) has to be a document. At least that's how I understand the object model: http://www.afralisp.net/reference/autocad-object-model.php#

So could you please try:

$oAcad = ObjGet("","AutoCAD.Application.20")
$oDoc = $oAcad.ActiveDocument
$oMS = $oDoc.Modelspace
$vTxt = $oMS.AddMTxt($aCoords, $width, $text)

 

Expand  

Nothing

I think I'm going to give up, I was wanting to create an autocad UDF but I do not think I'll be able to!

 

Thank you very much for the help

Posted

Nothing? Not even error messages?

Before creating an AutoCad UDF you need to know how the COM object model works. And how this is implemented in AutoIt.

My UDFs and Tutorials:

  Reveal hidden contents

 

  • 2 years later...
Posted

Reviving this topic, as I'm also trying get AutoCAD communication going.

What I'm stuck with how to define "three-element array of doubles"

  Quote
RetVal = object.AddPoint(Point)
object

Type: Block, ModelSpace, PaperSpace

The objects this method applies to.

Point

Access: Input-only

Type: Variant (three-element array of doubles)

The coordinates of the point to be created.

Expand  

https://knowledge.autodesk.com/search-result/caas/CloudHelp/cloudhelp/2019/ENU/AutoCAD-ActiveX-Reference/files/GUID-265E706F-78E4-48C6-B1AB-66FD794BF230-htm.html

VBA:

Sub Example_AddPoint()
    ' This example creates a point in model space.
    Dim pointObj As AcadPoint
    Dim location(0 To 2) As Double
    
    ' Define the location of the point
    location(0) = 5#: location(1) = 5#: location(2) = 0#
    
    ' Create the point
    Set pointObj = ThisDrawing.ModelSpace.AddPoint(location)
    ZoomAll
    
End Sub

Autoit:

Dim $location[3] = [5,5,0]
$oAcad = ObjGet("","AutoCAD.Application")
$oAcad.ActiveDocument.Modelspace.AddPoint($location)

Getting error:

  Quote

1.au3" (3) : ==> The requested action with this object has failed.:
$oAcad.ActiveDocument.Modelspace.AddPoint($location)
$oAcad.ActiveDocument.Modelspace^ ERROR

Expand  

 

Posted

as first step try this:

_Example()
Func _Example()
    Local $location[3] = [5,5,0]
    Local $oAcad = ObjGet("","AutoCAD.Application")
    If @error Then MsgBox($MB_ICONERROR, 'ObjGet', '@error = ' & @error & @CRLF & '@extended = ' & @extended)
    $oAcad.ActiveDocument.Modelspace.AddPoint($location)
EndFunc

 

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted (edited)

ObjGet doesn't produce the error, it fails during execution of AddPoint(), therefor no message box shown.

So with error handler I get this:

Local $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc")

Local $location[3] = [5,5,0]
Local $oAcad = ObjGet("","AutoCAD.Application")
$oAcad.ActiveDocument.Modelspace.AddPoint($location)

Func _ErrFunc($oError)
    ConsoleWrite(@ScriptName & " (" & $oError.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _
            @TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _
            @TAB & "err.windescription:" & @TAB & $oError.windescription & @CRLF & _
            @TAB & "err.description is: " & @TAB & $oError.description & @CRLF & _
            @TAB & "err.source is: " & @TAB & @TAB & $oError.source & @CRLF & _
            @TAB & "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
            @TAB & "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
            @TAB & "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
            @TAB & "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
            @TAB & "err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF)
EndFunc   ;==>_ErrFunc
  Quote

1.au3 (5) : ==> COM Error intercepted !
    err.number is:         0x80020009
    err.windescription:    Exception occurred.

    err.description is:     
    err.source is:         
    err.helpfile is:     
    err.helpcontext is:     0
    err.lastdllerror is:     0
    err.scriptline is:     5
    err.retcode is:     0x80070057

Expand  

 

Again, not much useful information, but definitely correct function name..so it must be wrong datatype of $location variable used with AddPoint() function.

Edited by VAN0
Posted

like this:
 

_Example()

Func _Example()
    Local $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc")

    Local $location[3] = [5,5,0]
    Local $oAcad = ObjGet("","AutoCAD.Application")
    Local $oActiveDocument = $oAcad.ActiveDocument
    If @error Then MsgBox($MB_ICONERROR, '$oAcad.ActiveDocument', '@error = ' & @error & @CRLF & '@extended = ' & @extended)

    Local $oModelspace = $oActiveDocument.Modelspace.AddPoint($location)
    If @error Then MsgBox($MB_ICONERROR, '$oActiveDocument.Modelspace.AddPoint', '@error = ' & @error & @CRLF & '@extended = ' & @extended)

    $oModelspace.AddPoint($location)
    If @error Then MsgBox($MB_ICONERROR, '$oModelspace.AddPoint', '@error = ' & @error & @CRLF & '@extended = ' & @extended)
    
EndFunc

Func _ErrFunc($oError)
    ConsoleWrite(@ScriptName & " (" & $oError.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _
            @TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _
            @TAB & "err.windescription:" & @TAB & $oError.windescription & @CRLF & _
            @TAB & "err.description is: " & @TAB & $oError.description & @CRLF & _
            @TAB & "err.source is: " & @TAB & @TAB & $oError.source & @CRLF & _
            @TAB & "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
            @TAB & "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
            @TAB & "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
            @TAB & "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
            @TAB & "err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF)
EndFunc   ;==>_ErrFunc

 

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted

You are passing Integers whereas the function expects Doubles.

Global $iInt = 1, $fFloat = 1.0
MsgBox(0, "Integer", "$iInt: " & VarGetType($iInt) & @CRLF & "$fFloat: " & VarGetType($fFloat))

So i think you should use:

Local $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc")

Local $location[3] = [5.0, 5.0, 0.0]
Local $oAcad = ObjGet("","AutoCAD.Application")
$oAcad.ActiveDocument.Modelspace.AddPoint($location)

Func _ErrFunc($oError)
    ConsoleWrite(@ScriptName & " (" & $oError.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _
            @TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _
            @TAB & "err.windescription:" & @TAB & $oError.windescription & @CRLF & _
            @TAB & "err.description is: " & @TAB & $oError.description & @CRLF & _
            @TAB & "err.source is: " & @TAB & @TAB & $oError.source & @CRLF & _
            @TAB & "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
            @TAB & "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
            @TAB & "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
            @TAB & "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
            @TAB & "err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF)
EndFunc   ;==>_ErrFunc

 

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted

@water still same error. So far I've tried send it as text, as autocad "array" - "(5.0,5.0,0.0)" each time same type of error.

@mLipok

Two popups:

image.png.0db04992e0de43b7eb40acd4949cb364.pngimage.png.c4f96a4e22c68b2f5524a9da2e0f3dad.png

 

  Quote

1.au3 (11) : ==> COM Error intercepted !
    err.number is:         0x80020009
    err.windescription:    Exception occurred.

    err.description is:     
    err.source is:         
    err.helpfile is:     
    err.helpcontext is:     0
    err.lastdllerror is:     0
    err.scriptline is:     11
    err.retcode is:     0x80070057
1.au3 (14) : ==> COM Error intercepted !
    err.number is:         0x000000A9
    err.windescription:    Variable must be of type 'Object'.
    err.description is:     
    err.source is:         
    err.helpfile is:     
    err.helpcontext is:     
    err.lastdllerror is:     0
    err.scriptline is:     14
    err.retcode is:     0x00000000

Expand  

 

Posted

Get some more detailed error information by combining what we already have:

Local $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc")
Local $location[3] = [5.0, 5.0, 0.0]
Local $oAcad = ObjGet("","AutoCAD.Application")
Local $oActiveDocument = $oAcad.ActiveDocument
If @error Then Exit MsgBox(0, "Error", "ActiveDocument returned: @error(hex) = " & Hex(@error) & ", @extended = " & @extended)
Local $oModelSpace = $oActiveDocument.Modelspace
If @error Then Exit MsgBox(0, "Error", "ModelSpace returned: @error(hex) = " & Hex(@error) & ", @extended = " & @extended)
$oModelSpace.AddPoint($location)
If @error Then Exit MsgBox(0, "Error", "AddPoint returned: @error(hex) = " & Hex(@error) & ", @extended = " & @extended)

Func _ErrFunc($oError)
    ConsoleWrite(@ScriptName & " (" & $oError.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _
            @TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _
            @TAB & "err.windescription:" & @TAB & $oError.windescription & @CRLF & _
            @TAB & "err.description is: " & @TAB & $oError.description & @CRLF & _
            @TAB & "err.source is: " & @TAB & @TAB & $oError.source & @CRLF & _
            @TAB & "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
            @TAB & "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
            @TAB & "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
            @TAB & "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
            @TAB & "err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF)
EndFunc   ;==>_ErrFunc

 

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted

@water It doesn't show any more information then the previous attempts. It shows AdPoint failed and that's all.

If I remove everything after it's name

$oAcad.ActiveDocument.Modelspace.AddPoint

it shows this error
 

  Quote

 

1.au3 (5) : ==> COM Error intercepted !
    err.number is:         0x8002000E
    err.windescription:    Invalid number of parameters.

    err.description is:     
    err.source is:         
    err.helpfile is:     
    err.helpcontext is:     
    err.lastdllerror is:     0
    err.scriptline is:     5
    err.retcode is:     0x00000000

 

Expand  

Which, I think, only proves that datatype in $location variable is wrong.

Posted
  On 12/5/2018 at 8:46 AM, VAN0 said:

1.au3 (11) : ==> COM Error intercepted !
    err.number is:         0x80020009
    err.windescription:    Exception occurred.

    err.description is:     
    err.source is:         
    err.helpfile is:     
    err.helpcontext is:     0
    err.lastdllerror is:     0
    err.scriptline is:     11
    err.retcode is:     0x80070057

Expand  

0x80070057 stands for E_INVALIDARG

I searched the web for "HRESULT 0x80070057 autocad". Got a few hits but nothing seems to describe the problem.
Then i searched the forum for the HRESULT. I think the following post (read the last few lines) describes what goes on. It seems to be an internal conversion problem with AutoIt and AutoCAD expecting a different format. Unfortunately what @LarsJ descibes is way over my head. Maybe he has a solution for this problem.

 

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted

Credit for identifying the problem.

AutoIt arrays are internally stored as safearrays of variants where the array type is VT_ARRAY + VT_VARIANT.

If you create an AutoIt array of doubles this way

Local $aDoubles = [ 5.0, 5.0, 0.0 ]

then $aDoubles is stored as a safearray of 3 variants where each variant contains a double. This means that the type of the variant is VT_R8 which is the same as double, and the data of the variant is the value of the double. The type of the safearray is still VT_ARRAY + VT_VARIANT.

But AddPoint method does not take a safearray of variants as input. It takes a safearray of doubles as input. That is a safearray of type VT_ARRAY + VT_R8. That's the reason for the invalid argument error.

Although it is possible to create a safearray of doubles in AutoIt with Windows API functions, it's not possible to pass the array to the AddPoint method because of the internal COM conversions that only work for safearrays of variants.

To avoid the internal COM conversions you can call AddPoint directly with DllCallAddress() through a pointer to the method. You can get this pointer in the VTABLE, and of course you also need a pointer directly to the VTABLE.

Although you can find all the details in the link above, it's very tricky. It appears to be easier to use VBA code.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...