darkshark

Convert VBA Autocad to Autoit

10 posts in this topic

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

Share this post


Link to post
Share on other sites



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


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2016-08-18 - Version 1.4.6.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2016-07-20 - Version 1.2.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
10 minutes ago, water said:

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

"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!

Share this post


Link to post
Share on other sites

Maybe

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

or

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

 


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2016-08-18 - Version 1.4.6.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2016-07-20 - Version 1.2.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
59 minutes ago, water said:

Maybe

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

or

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

 

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

Share this post


Link to post
Share on other sites

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 .....


NEW:     ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button:

Spoiler

Last update: 2016-11-12
Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft Components * TeamViewer API -UDF * JavaManagement UDF * VIES over SOAPPOP3.au3 UDF *  RTF Printer - UDF * XML.au3 - BETA * ADO.au3 UDF SMTP Mailer UDF * WinSCP UDF *

My contribution to others projects: * _sql.au3 UDF  *

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Best coding practices * 

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities *

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, The Art of Computer Programming
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

 

Share this post


Link to post
Share on other sites

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"

 

 

Share this post


Link to post
Share on other sites

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:

Spoiler

UDFs:
Active Directory (2016-08-18 - Version 1.4.6.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2016-07-20 - Version 1.2.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
8 hours ago, 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)

 

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

Share this post


Link to post
Share on other sites

#10 ·  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:

Spoiler

UDFs:
Active Directory (2016-08-18 - Version 1.4.6.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2016-07-20 - Version 1.2.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

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