Versions Compared

Key

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

...

Code Block
languagexml
<Project Sdk="Microsoft.NET.Sdk">

	<PropertyGroup>
		<TargetFramework>net8.0-windows</TargetFramework>
		<UseWindowsForms>true</UseWindowsForms>
		<ImplicitUsings>enable</ImplicitUsings>
		<Nullable>enable</Nullable>
		<EnableDynamicLoading>true</EnableDynamicLoading>
		<Platforms>AnyCPU;x64</Platforms>
	</PropertyGroup>

	<ItemGroup>
	  <Reference Include="CSiAPIv1">
	    <HintPath>..\..\..\..\..\API\CSI.API\bin\Debug\CSiAPIv1.dll</HintPath>
		<Private>false</Private>
		<ExcludeAssets>runtime</ExcludeAssets>
	  </Reference>
	</ItemGroup>

</Project>


6. With the modifications to the project file, you should be able to add a new Windows Form to your project

Image Added


7. The exact layout of the plugin form is not important, but for the copy and paste code in the steps below to operate correctly, the form must be named Form1, and must contain a Button control named button1 and RichTextBox control named richTextBox1

Image Added


8. Open the Form1.cs file and paste in the following code


Code Block
languagec#
using CSiAPIv1;

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
            {
                CreateStructure(richTextBox1.Text);
            }
            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 the Class1.cs file to cPlugin.cs and paste in the following code. Please pay attention to the comments in the code, they contain important information about correct plugin operation.

...