|
<< Click to Display Table of Contents >> Navigation: Programming Mechworks PDM > .NET API > MWAPI - API Example |
MechWorks .NET API
The new API can be used in any programming context since there is no dependency from any existing library.
To show CAD or DBWorks Standalone in process execution, the following examples are provided:
•Utility Script Example: in the installation path LST_DIR\DotNETAPI\Example there are some vb examples to show interaction with new API. To execute:
oCopy .vb file in LST_DIR
oExecute the script in DBWorks
•Event Script Example: in the installation path, the folder LST_DIR\DotNETAPI\Source contains the project with all the example code (to allow additional user customization). The output built module LST_DIR\DotNETAPI\MwPDMEvents.dll is also available together with the template LST_DIR\DotNETAPI\On_ScriptTemplate_.lst for the lst creation.
To execute:
oCopy LST_DIR\DotNETAPI\MwPDMEvents.dll file in LST_DIR
oCopy LST_DIR\DotNETAPI\On_ScriptTemplate_.lst file in LST_DIR and rename it according to the specific event
oIn DBWorks, activate the specific option and execute
With respect of previous scripting methodology, main aspects are:
•The .NET API object model encapsulates all available functionalities (no need to access DBWShell/DBWLib)
•No DBWInit: The MwPDMApi.Initialize object takes care to make all the required initialization starting the connection to the specified CAD or DBWorks Standalone;
•No DBWInput, DBWOutput management: in the .NET API object model, all parameters are strongly typed for the input/output management. In particular, in the Event Script management there is a class definition for each event with its own input and output data.
For example: regarding the OnOk DBWorks event there is the corresponding OnOk API class, managing as input the typed MWEventIN.DataForm.ONOK (EDIT, INSERT,...) and as output the typed MWEventOUT.DataForm.ONOK (OKDATA,...) data.
•No DBWResult management: in the .NET API object model, the output/return values are managed contextually. For generic issues (like CADSystem.Save), the return value of type ShellResult object allows to access more items regarding the specific method.
For completeness, here are the steps to write a simple VB.Net console application as an example of the out of process execution:
•Start MSDev → New Project → Visual Basic → Console Application (selecting .NET Framework 4)
•In the project references, add reference to MwPDMApi.dll
•In the Sub Main() type the following code:
VB version
Sub Main()
Dim pDBWInit As New MwPDMApi.Initialize()
' StandAlone or running CAD
Dim pAppl As MwPDMApi.Application=pDBWInit.StartConnection("DBWorks Standalone")
If (IsNothing(pAppl) = False) Then
pAppl.UserInterface.MsgBox("Running out of process")
Dim docUId As String = ""
pAppl.Selection.CurrentDocument(docUId)
If (docUId <> "") Then
Dim pDB As Object = pAppl.Database
Dim fname As String = pDB.QueryByUid(docUId, "FILE_NAME")
Dim fdir As String = pDB.QueryByUid(docUId, "FILE_DIRECTORY")
pAppl.UserInterface.MsgBox("DOCUMENT PATH: " & fdir & fname)
End If
End If
pDBWInit.EndConnection()
End Sub
C# version
static void Main(string[] args)
{
Type mwType = Type.GetTypeFromProgID("MwPDMApi.Initialize");
dynamic pDBWInit = Activator.CreateInstance(mwType);
// StandAlone or running CAD
dynamic pAppl = pDBWInit.StartConnection("DBWorks Standalone");
if (pAppl != null) {
pAppl.UserInterface().MsgBox("Running out of process");
string docUId = "";
pAppl.Selection().CurrentDocument(ref docUId);
if (docUId != "") {
dynamic pDB = pAppl.Database();
string fname = pDB.QueryByUid(docUId, "FILE_NAME");
string fdir = pDB.QueryByUid(docUId, "FILE_DIRECTORY");
pAppl.UserInterface().MsgBox("DOCUMENT PATH: " + fdir + fname);
}
pDBWInit.EndConnection();
}
}
•Build project
•With DBWorks Standalone running, start the Console Application execution.