Altova StyleVision 2024 Enterprise Edition

Sie können mit Hilfe von C# auf die Funktionalitäten der Application API zugreifen. Zur Erstellung des C#-Code können Sie Visual Studio 2012/2013/2015/2017/2019/2022 verwenden und den Code in einem Visual Studio-Projekt speichern. Erstellen Sie das Projekt folgendermaßen:

 

1.Fügen Sie in Microsoft Visual Studio mit dem Befehl Datei | Neu | Projekt ein neues Projekt hinzu.

2.Fügen Sie durch Auswahl des Befehls Projekt | Verweis hinzufügen hinzufügen eine Referenz zur StyleVision-Typbibliothek hinzu. Daraufhin wird das Dialogfeld "Verweis hinzufügen" angezeigt. Navigieren Sie zur StyleVision-Typbibliothekkomponente, die sich im StyleVision-Applikationsordner befindet und fügen Sie sie hinzu.

3.Geben Sie den gewünschten Code ein.

4.Kompilieren Sie den Code und führen Sie ihn aus.

 

C#-Beispielprojekt

Das StyleVision-Paket enthält ein C#-Beispielprojekt, das Sie im Unterordner API\C# des Ordners Examples finden:

 

 

Windows 7, Windows 8, Windows 10, Windows 11

C:\Benutzer\<Benutzername>\Dokumente\
Altova\StyleVision\2024\%APPNAME%>Examples

 

Sie können das Projekt innerhalb von Visual Studio 2012/2013/2015/2017/2019/2022 kompilieren und ausführen.

 

Anhand der unten stehenden Codefragmente wird gezeigt, wie Sie Grundfunktionen der Applikation verwenden können. Der Code ist dem Code im C#-Beispielprojekt im Ordner API Examples Ihres Applikationsordners ähnlich, kann sich aber geringfügig davon unterscheiden.

 

Platformkonfiguration

Wenn Sie ein 64-Bit-Betriebssystem haben und eine 32-Bit-Installation von StyleVision verwenden, müssen Sie die x86-Plattform im Konfigurationsmanager der Projektmappe hinzufügen und das Beispiel mit dieser Konfiguration erstellen. Im Dialogfeld "Neue Projektmappenplattform" kann eine neue x86-Plattform (für die aktive Projektmappe in Visual Studio) erstellt werden (Build | Konfigurations-Manager | Aktive Projektmappenplattform: | <Neu…>).

 

Funktionen im Codebeispiel unten

In diesem Beispiel sehen Sie eine einfache Benutzeroberfläche (Abbildung unten) mit Schaltflächen, über die grundlegende StyleVision-Operationen aufgerufen werden:

 

CSInterfaceFormSV

 

Start StyleVision: Startet StyleVision, das als Automation Server registriert ist, oder aktiviert das Programm, wenn StyleVision bereits ausgeführt wird.

Open OrgChart.pxf: Navigiert zu einem der mitStyleVision installierten Beispieldokumente und öffnet es. Wenn das Dokument bereits offen ist, wird es zum aktiven Dokument.

Open MultiFileOutput.sps: Öffnet ein weiteres Beispieldokument.

Shutdown StyleVision: Beendet StyleVision.

 

 

Sie können den Code (des Codefragments unten oder des C#-Beispiels aus dem Ordner API Examples) beliebig modifizieren und ausführen.

 

Kompilieren und Ausführen des Beispiels

Doppelklicken Sie im Ordner API Examples auf die Datei AutomateStyleVision_VS2008.sln bzw. auf die Datei AutomateStyleVision_VS2010.sln (um sie in Visual Studio 2012/2013/2015/2017/2019/2022 zu öffnen). Alternativ dazu können Sie die Datei auch von Visual Studio aus öffnen (mit dem Befehl Datei | Öffnen | Projekt/Projektmappe). Um das Beispiel zu kompilieren und auszuführen wählen Sie Debuggen | Debuggen starten bzw. Debuggen | Starten ohne Debugging.

 

Code des Beispiels

Nachstehend sehen Sie den C#-Code der Grundfunktionen des im AutomateStyleVision Beispiel erstellten Formulars (Form1.cs). Beachten Sie, dass ich der unten aufgelistete Code geringfügig von dem Code im Formular unter API Examples unterscheiden kann. Der Code ist zum besseren Verständnis mit Kommentaren versehen. Abhängig davon, welche Application API-Funktionen sie aufrufen, sind Teile dieses Codes auch separat in Unterabschnitten dieses Abschnitts beschrieben.

 

Der Code besteht im Wesentlichen aus einer Reihe von Handlern für die Schaltflächen der oben gezeigten Benutzeroberfläche.

 

 

namespace WindowsFormsApplication2

{

   public partial class Form1 : Form

   {

       public Form1()

       {

           InitializeComponent();

       }

 

       // An instance of StyleVision is accessed via its automation interface.

       StyleVisionLib.Application StyleVision;

 

       // Location of examples installed with StyleVision

       String strExamplesFolder;

 

       private void Form1_Load(object sender, EventArgs e)

       {

           // Locate examples installed with StyleVision.

           // REMARK: You might need to adapt this if you have a different major version of the product.

           strExamplesFolder = Environment.GetEnvironmentVariable("USERPROFILE") + "\\My Documents\\Altova\\StyleVision2012\\StyleVisionExamples\\";

       }

 

       // Handler for the "Start StyleVision" button

       private void StartStyleVision_Click(object sender, EventArgs e)

       {

           if (StyleVision == null)

           {

               Cursor.Current = Cursors.WaitCursor;

 

               // If there is no StyleVision instance, create one and make it visible.

               StyleVision = new StyleVisionLib.Application();

               StyleVision.Visible = true;

 

               Cursor.Current = Cursors.Default;

           }

           else

           {

               // If a StyleVision instance is already running, make sure it's visible.

               if (!StyleVision.Visible)

                   StyleVision.Visible = true;

           }

       }

 

       // Handler for the "Open OrgChart.pxf" button

       private void openOrgChart_Click(object sender, EventArgs e)

       {

           // Make sure there's a running StyleVision instance, and that it's visible

           StartStyleVision_Click(null, null);

 

           // Open a sample files installed with the product.

           StyleVision.Documents.OpenDocument(strExamplesFolder + "OrgChart.pxf");

           updateListBox();

       }

 

       // Handler for the "Open MultiFileOutput.sps" button

       private void openMultiFileOutput_Click(object sender, EventArgs e)

       {

           // Make sure there's a running StyleVision instance, and that it's visible

           StartStyleVision_Click(null, null);

 

           // Open one of the sample files installed with the product.

           StyleVision.Documents.OpenDocument(strExamplesFolder + "MultiFileOutput.sps");

           updateListBox();

       }

 

       // Handler for the "Shutdown StyleVision" button

       // Shut down the application instance by explicitly releasing the COM object.

       private void shutdownStyleVision_Click(object sender, EventArgs e)

       {

           if (StyleVision != null)

           {

               // Allow shut-down of StyleVision by releasing UI

               StyleVision.Visible = false;

 

               // Explicitly release the COM object

               try

               {

                   int i = System.Runtime.InteropServices.Marshal.ReleaseComObject(StyleVision);

                   while (System.Runtime.InteropServices.Marshal.ReleaseComObject(StyleVision) > 0) ;

               }

               finally

               {

                   // Disallow subsequent access to this object.

                   StyleVision = null;

               }

           }

       }

 

       delegate void addListBoxItem_delegate(string sText);

       // Called from the UI thread

       private void addListBoxItem(string sText)

       {

           listBoxMessages.Items.Add(sText);

       }

       // Wrapper method to call UI control methods from a worker thread

       void syncWithUIthread(Control ctrl, addListBoxItem_delegate methodToInvoke, String sText)

       {

           // Control.Invoke: Executes on the UI thread, but calling thread waits for completion before continuing.

           // Control.BeginInvoke: Executes on the UI thread, and calling thread doesn't wait for completion.

           if (ctrl.InvokeRequired)

               ctrl.BeginInvoke(methodToInvoke, new Object[] { sText });

       }

 

       // Event handler for OnDocumentClosed event

       private void handleOnDocumentClosed(StyleVisionLib.Document i_ipDocument)

       {

           String sText = "";

 

           if (i_ipDocument.Name.Length > 0)

               sText = "Document " + i_ipDocument.Name + " was closed!";

 

           // Synchronize the calling thread with the UI thread because

           // COM events are triggered from a working thread

           addListBoxItem_delegate methodToInvoke = new addListBoxItem_delegate(addListBoxItem);

           // Call syncWithUIthread with the following arguments:

           // 1 - listBoxMessages - list box control to display messages from COM events

           // 2 - methodToInvoke  - a C# delegate which points to the method which will be called from the UI thread

           // 3 - sText           - the text to be displayed in the list box

           syncWithUIthread(listBoxMessages, methodToInvoke, sText);

       }

 

 

 

       private void updateListBox()

       {

           // Iterate through all open documents

           listBoxMessages.Items.Clear();

 

 

           for (int i = 1; i <= StyleVision.Documents.Count; i++)

           {

               StyleVisionLib.Document doc = StyleVision.Documents[i];

 

               if (doc != null)

               {

                   if (checkBoxEventOnOff.Checked)

                       doc.OnDocumentClosed += new StyleVisionLib._IDocumentEvents_OnDocumentClosedEventHandler(handleOnDocumentClosed);

                   else

                       doc.OnDocumentClosed -= new StyleVisionLib._IDocumentEvents_OnDocumentClosedEventHandler(handleOnDocumentClosed);

 

                   listBoxMessages.Items.Add(doc.Name);

                   StyleVisionLib.SchemaSources sources = doc.SchemaSources;

 

                   for (int j = 1; j <= sources.Count; j++)

                   {

                       StyleVisionLib.SchemaSource source = sources[j];

 

                       if (source != null)

                       {

                           listBoxMessages.Items.Add("\tSchema      file name : " + source.SchemaFileName + "\");

                           listBoxMessages.Items.Add("\tWorking XML file name : " + source.WorkingXMLFileName + "\");

                           listBoxMessages.Items.Add("\tIs main schema source : " + source.IsMainSchemaSource + "\tType name : " + source.TypeName + "\");

                       }

                   }

               }

           }

       }

   }

}

 

© 2017-2023 Altova GmbH