...
| Code Block | ||
|---|---|---|
| ||
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);
//MessageBox.Show(richTextBox1.Text + " == " + deserializedText + " : " + (richTextBox1.Text == deserializedText));
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 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.
...