hexagon logo

PC-Dmis Automation C#

So I retired as of last July, but had all this stuff about PC-Dmis Automation which I've finally got around to uploading. Don't know it it is of any interest, but here it is.

The zip file has to Visual Studio project folders, both written in C#. One folder is a Class Library which has all the code for connecting with and dynamically interacting with PC-Dmis. The second folder is a C# application with uses the Class Library to connect and interact with PC-Dmis.

I also included two documents one explains how to setup the two Projects the other is about the application program.

Rather than trying to anticipate every possible question, the provided documents should be sufficient for someone familiar with visual studio and C#. Otherwise, I'll watch for questions to show up here. I can't promise to answer every question, given the complexity of what the answer might require. But I have taken phone calls and zoom opens up the possibility to demo live.

Good Luck

https://drive.google.com/drive/folders/1EDMe0GvN6TC_0Fcr_7Zt7_UqH-EV_8o2?usp=sharing
Parents
  • This is the source code for the above tolerance test program. As I mentioned earlier, it's basically the 'Barebones' code with some added code for the additional stuff in the user interface and the processing of PC-Dmis commands. The following code is the 'BareBones' Form1 source code renamed to MainForm. The second code listing is another 'partial class' source code file named 'PcdmisHelper' in this file I moved the code that was in the original Form1.cs that was specifically having to do with PC-Dmis into this new file. Then I added the code (see the ReplaceToleranceValues method) that made the modifications to the Dimension commands.

    Code from the MainForm.cs file.
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace UpdateTolerances
    {
    public partial class MainForm : Form
    {
    public MainForm()
    {
    InitializeComponent();
    
    // Update Pcdmis connection status
    UpdatePcdmisStatus();
    }
    
    /// <summary>
    /// This method handles the click event for the top button.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void btnStartPcdmis_Click(object sender, EventArgs e)
    {
    // Disable the PC-Dmis connect button to prevent multiple clicks
    // while PC-Dmis is trying to start.
    btnStartPC_Dmis.Enabled = false;
    
    if (pcdSession is null)
    {
    Func<Boolean> method = Connect; // Assign the Connect method to a function delegate called 'method'.
    method.BeginInvoke(ConnectionDone, method); // Launch PC-Dmis in another thread.
    }
    
    // This method is finished immediately after the above Invoke statement. It does
    // not wait around for PC-Dmis to start. While PC-Dmis is starting up, every part
    // of this application and UI are still active and not 'locked' up.
    }
    
    /// <summary>
    /// A method to manage the use of a textbox to display the connection status of
    /// PC-Dmis.
    /// </summary>
    private void UpdatePcdmisStatus()
    {
    if (PcdmisIsConnected)
    {
    txbStatus.BackColor = Color.Green; // set the status textbox to green
    btnStartPC_Dmis.Enabled = false; // disable button to prevent 2nd connection.
    }
    else
    {
    txbStatus.BackColor = Color.Red;
    btnStartPC_Dmis.Enabled = true;
    }
    }
    
    /// <summary>
    /// Method to return true or false according to the state of pcdSession variable.
    /// </summary>
    private Boolean PcdmisIsConnected
    {
    get
    {
    if (pcdSession is null)
    {
    return false;
    }
    else
    {
    return true;
    }
    }
    }
    
    /// <summary>
    /// This method is a listener to the 'View Pcdmis' button click event. When this button
    /// is clicked this method flips between displaying and hiding the PC-Dmis window.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void btnViewPcdmis_Click(object sender, EventArgs e)
    {
    // Always confirm you have a connected session first
    if (!PcdmisIsConnected) return;
    
    if (pcdSession.Visible)
    {
    // make it invisible)
    pcdSession.Visible = false;
    btnViewPcdmis.Text = "View PC-Dmis Window";
    }
    else
    {
    // make it visible
    pcdSession.Visible = true;
    btnViewPcdmis.Text = "Unview PC-Dmis Window";
    }
    }
    
    /// <summary>
    /// Listener method for the form closing event. Handles the proper closing of the PC-Dmis
    /// session. Without this method PC-Dmis will continue living in the background after this
    /// application is closed and exited.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void MainForm_Closing(object sender, FormClosingEventArgs e)
    {
    // Close the PCDLRN runtime service when this application is shutdown.
    if (pcdSession != null)
    {
    pcdSession.UserExit = true;
    pcdSession.Quit();
    pcdSession = null;
    }
    }
    
    private String pcdProgramPath { get; set; }
    
    private void openToolStripMenuItem_Click(object sender, EventArgs e)
    {
    OpenFileDialog.Filter = "prg files (*.prg)|*.prg|All files (*.*)|*.*";
    OpenFileDialog.RestoreDirectory = true;
    
    if (OpenFileDialog.ShowDialog() == DialogResult.OK)
    {
    pcdProgramPath = OpenFileDialog.FileName;
    }
    else
    {
    pcdProgramPath = "";
    }
    }
    
    private void btnStartTest_Click(object sender, EventArgs e)
    {
    if (pcdProgramPath == "") return;
    
    if (OpenPcdmisProgram(pcdProgramPath))
    {
    //simple check for the presence of tolerance values.
    if (txbPlusValue.Text == "" || txbMinusValue.Text == "")
    {
    MessageBox.Show("Missing tolerance values", "Bad Tolerances",
    MessageBoxButtons.OK, MessageBoxIcon.Error);
    return;
    }
    
    double plus = Convert.ToDouble(txbPlusValue.Text);
    double minus = Convert.ToDouble(txbMinusValue.Text);
    
    StartClock();
    ReplaceToleranceValues(plus, minus);
    StopClock();
    }
    else
    {
    MessageBox.Show("Unable to open the selected program.",
    "Open Program Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    
    }
    
    private void StartClock()
    {
    DateTime clockTime = DateTime.Now;
    txbStartTime.Text = clockTime.ToLongTimeString();
    }
    
    private void StopClock()
    {
    DateTime clockTime = DateTime.Now;
    txbStopTime.Text = clockTime.ToLongTimeString();
    }
    
    private void saveToolStripMenuItem_Click(object sender, EventArgs e)
    {
    if (pcdProgram is null) return;
    
    pcdProgram.Save();
    }
    }
    }
    

Reply
  • This is the source code for the above tolerance test program. As I mentioned earlier, it's basically the 'Barebones' code with some added code for the additional stuff in the user interface and the processing of PC-Dmis commands. The following code is the 'BareBones' Form1 source code renamed to MainForm. The second code listing is another 'partial class' source code file named 'PcdmisHelper' in this file I moved the code that was in the original Form1.cs that was specifically having to do with PC-Dmis into this new file. Then I added the code (see the ReplaceToleranceValues method) that made the modifications to the Dimension commands.

    Code from the MainForm.cs file.
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace UpdateTolerances
    {
    public partial class MainForm : Form
    {
    public MainForm()
    {
    InitializeComponent();
    
    // Update Pcdmis connection status
    UpdatePcdmisStatus();
    }
    
    /// <summary>
    /// This method handles the click event for the top button.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void btnStartPcdmis_Click(object sender, EventArgs e)
    {
    // Disable the PC-Dmis connect button to prevent multiple clicks
    // while PC-Dmis is trying to start.
    btnStartPC_Dmis.Enabled = false;
    
    if (pcdSession is null)
    {
    Func<Boolean> method = Connect; // Assign the Connect method to a function delegate called 'method'.
    method.BeginInvoke(ConnectionDone, method); // Launch PC-Dmis in another thread.
    }
    
    // This method is finished immediately after the above Invoke statement. It does
    // not wait around for PC-Dmis to start. While PC-Dmis is starting up, every part
    // of this application and UI are still active and not 'locked' up.
    }
    
    /// <summary>
    /// A method to manage the use of a textbox to display the connection status of
    /// PC-Dmis.
    /// </summary>
    private void UpdatePcdmisStatus()
    {
    if (PcdmisIsConnected)
    {
    txbStatus.BackColor = Color.Green; // set the status textbox to green
    btnStartPC_Dmis.Enabled = false; // disable button to prevent 2nd connection.
    }
    else
    {
    txbStatus.BackColor = Color.Red;
    btnStartPC_Dmis.Enabled = true;
    }
    }
    
    /// <summary>
    /// Method to return true or false according to the state of pcdSession variable.
    /// </summary>
    private Boolean PcdmisIsConnected
    {
    get
    {
    if (pcdSession is null)
    {
    return false;
    }
    else
    {
    return true;
    }
    }
    }
    
    /// <summary>
    /// This method is a listener to the 'View Pcdmis' button click event. When this button
    /// is clicked this method flips between displaying and hiding the PC-Dmis window.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void btnViewPcdmis_Click(object sender, EventArgs e)
    {
    // Always confirm you have a connected session first
    if (!PcdmisIsConnected) return;
    
    if (pcdSession.Visible)
    {
    // make it invisible)
    pcdSession.Visible = false;
    btnViewPcdmis.Text = "View PC-Dmis Window";
    }
    else
    {
    // make it visible
    pcdSession.Visible = true;
    btnViewPcdmis.Text = "Unview PC-Dmis Window";
    }
    }
    
    /// <summary>
    /// Listener method for the form closing event. Handles the proper closing of the PC-Dmis
    /// session. Without this method PC-Dmis will continue living in the background after this
    /// application is closed and exited.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void MainForm_Closing(object sender, FormClosingEventArgs e)
    {
    // Close the PCDLRN runtime service when this application is shutdown.
    if (pcdSession != null)
    {
    pcdSession.UserExit = true;
    pcdSession.Quit();
    pcdSession = null;
    }
    }
    
    private String pcdProgramPath { get; set; }
    
    private void openToolStripMenuItem_Click(object sender, EventArgs e)
    {
    OpenFileDialog.Filter = "prg files (*.prg)|*.prg|All files (*.*)|*.*";
    OpenFileDialog.RestoreDirectory = true;
    
    if (OpenFileDialog.ShowDialog() == DialogResult.OK)
    {
    pcdProgramPath = OpenFileDialog.FileName;
    }
    else
    {
    pcdProgramPath = "";
    }
    }
    
    private void btnStartTest_Click(object sender, EventArgs e)
    {
    if (pcdProgramPath == "") return;
    
    if (OpenPcdmisProgram(pcdProgramPath))
    {
    //simple check for the presence of tolerance values.
    if (txbPlusValue.Text == "" || txbMinusValue.Text == "")
    {
    MessageBox.Show("Missing tolerance values", "Bad Tolerances",
    MessageBoxButtons.OK, MessageBoxIcon.Error);
    return;
    }
    
    double plus = Convert.ToDouble(txbPlusValue.Text);
    double minus = Convert.ToDouble(txbMinusValue.Text);
    
    StartClock();
    ReplaceToleranceValues(plus, minus);
    StopClock();
    }
    else
    {
    MessageBox.Show("Unable to open the selected program.",
    "Open Program Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    
    }
    
    private void StartClock()
    {
    DateTime clockTime = DateTime.Now;
    txbStartTime.Text = clockTime.ToLongTimeString();
    }
    
    private void StopClock()
    {
    DateTime clockTime = DateTime.Now;
    txbStopTime.Text = clockTime.ToLongTimeString();
    }
    
    private void saveToolStripMenuItem_Click(object sender, EventArgs e)
    {
    if (pcdProgram is null) return;
    
    pcdProgram.Save();
    }
    }
    }
    

Children
No Data