VBA
Discuss AutoCAD ActiveX and VBA (Visual Basic for Applications) questions here.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

use function in other dvb-files

6 REPLIES 6
Reply
Message 1 of 7
Milius
944 Views, 6 Replies

use function in other dvb-files

Hallo !

 

we want to create a library of multi-use-function in a seperate dvb-file. now our question is how to connect to this files (loaded or not active) and call functions of this library.

 

is this possible - how?

 

regards Jan 🙂

6 REPLIES 6
Message 2 of 7
norman.yuan
in reply to: Milius

YOu can simply set reference to that DVB in your current project.

 

That is, click menu "Tools->References...", and then click ""Browse..." button on the dialog box to navigate to the said DVB. Once the references to that DVB is set, all public methods (functions, procedures) then become available.

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 7
Milius
in reply to: norman.yuan

thanks for help !

Message 4 of 7
Milius
in reply to: Milius

hi !

 

i am again !

 

is there a possibilty to load dvb-files dynamically by code?

 

additional we have following problem. the developer creates files on 32bit machines. there is one user in time who use a 64bit machine. so we copy the 32-bit-dvb-file in a special folder. but the user must open manuall and save manual for use in 64bit. there may be a little change in the dvb-file by save in 64bit !

 

did someone know a automatically way for this problem ?

 

regards Jan 🙂

 

Message 5 of 7
norman.yuan
in reply to: Milius

Yes, there is way to load DVB file at runtime and call its macro:

 

AcadApplication.LoadDVB()

AcadApplication.RunMacro().

 

For example, I have a labrary DVB that defines an Add(a As Integer, b As Integer) As Interger function in a module, called "Calculation". the library saved as "MyTool.dvb";

Then I have another VBA project, saved as "VbaWork.dvb", that use the "MyTool.dvb" as reference. Then it has a public Sub called DoSomething() in model "MyWork". Inside the DoSomething(), the referenced MyTool.Calculation.Add() is used. That is, in this VBA project, a macro "MyWork.DoSomething" is defined.

 

Now, in any other project, if you need to run DoSomething(), you only need to do something like this:

 

Public Sub Test()

    Application.LoadDVB "C:\...\VbaWork.dvb"    

    Application.RunMacro "MyWork.DoSomething"    

End Sub

 

As for 32/64 bit issue, it is complicated, depending not only the OS being 32 or 64 bit, but also AutoCAD version.

 

AutoCAD VBA is 32bit, regardless AutoCAD itself being 32 or 64 bit, until AutoCAD2014. So, if you use Acad2013 or older, all the VBA code should be 32-bit and theoretically runs with both 32 and 64-bit. However, the code may still be different and not exhangeable between 32 and 64 bit. For exaple, if your code used ObjectId, then with 64-bit AutoCAD, even its VBA is 32 bit, the "ObjectId" has to be changed to "ObjectId32". There is also problem as you described that yo need to open the VBA manually and save it again, because the references to 32/64 bit AutoCAD type libarary saved in DVB file.

 

In my opinion, you may actually runinto more issues once you move to full 64-bit AutoCAD VBA (Acad2014 or later), if your VBA project is fairly complicated. If so, you may seariously reassess whether it is worth further investing more time to VBA development.

Norman Yuan

Drive CAD With Code

EESignature

Message 6 of 7
Milius
in reply to: norman.yuan

Hi Norman,

 

we build your example successful! Thanks.

 

But is there a practicable way to take parameters into the function call?

 

regards Jan 🙂

Message 7 of 7
norman.yuan
in reply to: Milius

There is no way to call a function that meeds data to be passed as parameters in a dynamically loaded VBA project. As the sample code I showed in previous reply, the only way to run the dynamically loaded VBA project is call Application.RunMacro(). That means only a public procedure defined i a module that does not take any parameter can be called as VBA Macro.

 

So, if the dynamically loaded VBA macro depends on some data produced by your current code execution, you need to find way to store the data somewhere so that when the other VBA module is loaded and its macro is executed, the data somehow is available: you can use system user variables (UserI1-5/UserR1-5/UserS1-5), or you can save the data to a data file... Something like

Sub DoSomeThing()

 

    Dim oldUserR1 As String

    oldUserR1=ThisDrawing.GetVariable("USERR1")

    Dim data As String

    data=GetParticularDataFromDrawing(ThisDrawing)

 

 

    If data="xxxxx" Then

       

        ThisDrawing.SetVariable("USERS1", data)

       Application.LoadVBA "C:\MyVBAProjects\TheOtherVBA.dvb"

       Application.RunMacro "MyModule.MyMacro"

     

       ThiDrawing.SetVariable("USERR1", oldUserR1)

 

    End If

 

End Sub

 

And in the "TheOtherVBA,dvb"''s "MyMacro" procedure, you then do this

 

Public Sub MyMaccro()

    Dim data As String

    data=ThisDrawing.GetVariable("USERR1")

    If Len(data)=0 Then

        'Do something

    Else

       'Do something else

    End If

End Sub

Norman Yuan

Drive CAD With Code

EESignature

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost