Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagec#
using CSiAPIv1;
using Newtonsoft.Json;

namespace CSiNET8PluginExample1
{
    public partial class Form1 : Form
    {

        protectedprivate cSapModel _sapModel;
        protectedprivate cPluginCallback _pluginCallback;
        private int errorCode = 0; // default return code is no error

        public Form1()
        {
            InitializeComponent();
            FormClosing += Form1_FormClosing;
        }

        public void SetSapModel(ref cSapModel inSapModel, ref cPluginCallback inPluginCallback)
        {
            _sapModel = inSapModel;
            _pluginCallback = inPluginCallback;

            richTextBox1.Text = "Hello CSI Friends";
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            // It is very important to call _pluginCallback.Finish(0errorCode) when the form closes, !!!
            // otherwise, the CSI program will wait and be hung !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            _pluginCallback.Finish(0errorCode);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                // Exercise some simple Newtonsoft.Json functionality
                string jsonText = JsonConvert.SerializeObject(richTextBox1.Text);
                string deserializedText = JsonConvert.DeserializeObject<string>(jsonText);
                
                CreateStructure(deserializedText);
            }
            catch (Exception ex)
            {
                MessageBox.Show("The following error terminated the Pluginoccurred:" + Environment.NewLine + ex.Message);
            }    errorCode = 2; // will be visible to  finallyplugin end-user for debugging purposes
            }
            finally
            {
                this.Close();
            }
        }

        public void CreateStructure(string text)
        {
            int CallResult;
            int i;
            string FrameName = "FrameName";

            var aVectorFont = new cVectorFont();
            double CharHeight = 200.0;
            var HAlignment = cVectorFont.TextAlignment.kTA_HLeft;
            var VAlignment = cVectorFont.TextAlignment.kTA_VTop;
            double[] textX = new double[1], textY = new double[1];
            int NumPts;

            // test for exception handling
            if (string.Equals(text, "crash", StringComparison.InvariantCultureIgnoreCase))
            {
                textX[99] = 0; // out of bounds
            }

            aVectorFont.FillTextVertices(text.ToUpper(), CharHeight, HAlignment, VAlignment, ref textX, ref textY);
            NumPts = textX.Length;

            CallResult = _sapModel.InitializeNewModel();
            CallResult = _sapModel.File.NewBlank();
            for (i = 0; i < NumPts - 2; i++)
            {
                FrameName = "FrameName" + (i / 2).ToString();
                CallResult = _sapModel.FrameObj.AddByCoord(textX[i], textY[i], 0, textX[i + 1], textY[i + 1], 0, ref FrameName);
                i++;
            }

            _sapModel.View.RefreshView(0, false);
        }
    }
}
   


9. In your project, locate the Class1.cs file and rename it to cPlugin.cs . Then open it and paste in the following code. Please pay attention to the comments in the code, they contain important information about correct plugin operation.

Code Block
languagec#
using CSiAPIv1;

namespace CSiNET8PluginExample1
{
    // Implementing the cPluginContract interface is not required, however
    // it is recommended to ensure that the required cPlugin methods are created correctly.
    // Do not implement the Info or Main methods explicitly,
    // i.e. their method signatures are correct as is
    public class cPlugin : cPluginContract
    {
        private static string _modality = "Non-Modal";
         private static string _versionString = "2.0";
        private public int Info(referrorCode string= Text)0; // default return code is no error

{        public int Info(ref string Text)
try        {
            try
            {
                Text = "This plugin is supplied by Computers and Structures, Inc., " +
                       "as a simple example for developers of new plugins for CSI products. " +
                       "It starts a new blank model, then converts a line of text into " +
                       "frame objects and adds them to the model. IfIt youtrivially enteruses the popular " +
                       "text \"crash\", an error will be generated for testing purposesNewtonsoft.Json library to demonstrate proper dependency management. " +
                       "VersionIf "you +enter _versionString;the " +
          }             catch (Exception)
            {"text \"crash\", an error will be generated for testing purposes. " +
               }        "Version " + _versionString;
  return 0;         }
          public  voidcatch Main(refException)
cSapModel sapModel, ref cPluginCallback pluginCallback)
        {
            var}
aForm
= new Form1();          return 0;
  try      }

     {   public              aForm.SetSapModel(refvoid Main(ref cSapModel sapModel, ref cPluginCallback pluginCallback);
        {
        if (string.Compare(_modality, "Non-Modal", true) ==var 0)aForm = new Form1();

            {try
            {
       // Non-modal form, allows graphics refresh operations in CSI program aForm.SetSapModel(ref sapModel, ref pluginCallback);

                if  // but Main will return to CSI program before the form is closed.(string.Compare(_modality, "Non-Modal", true) == 0)
                {
            aForm.Show();        // Non-modal form, allows graphics refresh operations in CSI }program, 
               else     // but Main will return to CSI program before the form is {closed.
                    // Modal form, will not return to CSI program until form is closed,aForm.Show();
                }
               // butelse
may cause errors when refreshing the view.          {
          aForm.ShowDialog();          // Modal form, will not return to }CSI program until form is closed,
                    // Itbut may iscause veryerrors importantwhen torefreshing callthe pluginCallback.Finish(iError) when the form closes, !!!view.
                    aForm.ShowDialog();
      // otherwise, the CSI program will wait and be hung !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! }

                // It is very important to call pluginCallback.Finish(errorCode) when the form closes, !!!
                // otherwise, the CSI program will wait and be hung !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                // This must be done inside the closing event for the form itself, not here !!!!!!!!!!!
 must be done inside the closing event for the form itself, not here !!!!!!!!!!!!!!

                // If you have only algorithmic code here without any forms, 
                // then call pluginCallback.Finish(errorCode) here before returning to the CSI program

                // errorCode = 0 indicates that the plugin completed successfully
                // ie pluginCallback.Finish(0)
                // errorCode = (Any non-zero integer) indicates that the plugin closed with an error
                // ie pluginCallback.Finish(1)
                // If youan simplyerror haveoccurs, algorithmicthe codeerrorCode herevalue withoutwill anybe forms,displayed
                 // thento the call pluginCallback.Finish(iError) here before returning to the CSI programplugin end-user in a message box, for debugging purposes.

                // If your code will run for more than a few seconds, you should exercise
                // the Windows messaging loop to keep the program responsive. You may 
                // also want to provide an opportunity for the user to cancel operations.

            }
            catch (Exception ex)
            {
                errorCode = 1;
                MessageBox.Show("The following error terminated the plugin:" + Environment.NewLine + ex.Message);

                // call Finish to inform the CSI program that the plugin has terminated
                try
                {
                    pluginCallback.Finish(1errorCode); // error code 1 will be visible to plugin end-user for debugging purposes
                }
                catch (Exception)
                {
                }
            }
        }
    }
}

...