Versions Compared

Key

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

...

    • Change the <TargetFramework> from net8.0 to net8.0-windows . This is necessary needed to allow our the plugin to open a instantiate Windows form forms (Note: this plugin has only been tested to work on the Windows platform)
    • Add the <UseWindowsForms>true</UseWindowsForms> attribute to the <PropertyGroup> . This will allow us to add a Windows form Form to our class library.
    • Add the <EnableDynamicLoading>true</EnableDynamicLoading> attribute to the <PropertyGroup> . This will allow the CSI product to load our project as a plugin. 

...

8. Open the Form1.cs file and paste in the following code. Don't worry about compilation errors, you'll add the other required classes in the following steps.

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

namespace CSiNET8PluginExample1
{
    public partial class Form1 : Form
    {

        protected cSapModel _sapModel;
        protected cPluginCallback _pluginCallback;

        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(0) when the form closes, !!!
            // otherwise, the CSI program will wait and be hung !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            _pluginCallback.Finish(0);
        }

        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 Plugin:" + Environment.NewLine + ex.Message);
            }
            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. Rename 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.

...