Results 1 to 2 of 2

Thread: Open PC-DMIS program dialog in VBScript

  1. #1
    PC-DMIS Guru
    • Additional Information
      • Years CMM Experience:
      • Since 1994
      • Software Version:
      • PC-DMIS CAD++ 2013 MR1 SP2
      • Machine Type:
      • CEJ12C, CEJ12000
      • Location:
      • Sweden
      • Job Function:
      • Quality tech., CMM programmer/operator
    vpt.se's Avatar
    Join Date
    Feb 2007
    Location
    Sweden
    Posts
    4,770

    Default Open PC-DMIS program dialog in VBScript

    Wrote this little script to ease the opening of PC-DMIS programs.
    All our programs are located on a network drive in folders named after the first digit in the partnumber (programname).

    As we are closing in on nearly 1000 programs, there can be a multitude of eyeballing the folders until the specific program
    are found.

    Code:
    Option Explicit
    Dim objFSO, objFolder, objShell, strDirectory, firstchar, InputFolder, found, serverpath, objDLG
    Dim PCDApp, PCDPartPrograms, PCDPartProgram
    
    'Dialog for programname/partnumber
    InputFolder = InputBox("Enter programname/partnumber: ", "Search program")
    'Get the first char in part/programname
    firstchar = Left(InputFolder, 1)
    'Hardcoded absolute serverpath = "\\server\searchpath\to\your_server\root\_PC-DMIS\Program"
    
    'Assign searchpath using the first char in part/programname
    Select Case firstchar
    	Case "1"
    			strDirectory = serverpath & "\1-\" & InputFolder
    	Case "2"
    			strDirectory = serverpath & "\2-\" & InputFolder
    	Case "3"
    			strDirectory = serverpath & "\3-\" & InputFolder
    	Case "4"
    			strDirectory = serverpath & "\4-\" & InputFolder
    	Case "5"
    			strDirectory = serverpath & "\5-\" & InputFolder
    	Case "6"
    			strDirectory = serverpath & "\6-\" & InputFolder
    	Case "7"
    			strDirectory = serverpath & "\7-\" & InputFolder
    	Case "8"
    			strDirectory = serverpath & "\8-\" & InputFolder
    	Case "9"
    			strDirectory = serverpath & "\9-\" & InputFolder
    End Select
    
    If InputFolder = "" Then
       WScript.Quit
    End If
    
    'Create filesystemobject
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    'Check if the folder exists
    If objFSO.FolderExists(strDirectory) Then
       Set objFolder = objFSO.GetFolder(strDirectory)
       'WScript.Echo InputFolder & " found!"
       found = 1
     Else
       WScript.Echo InputFolder & " was not found!"
       found = 0
    End If
    
    'If the folder existed, open it in Windows Explorer so the user can easily access the files in it
    If (found = 1) then
      Set objShell = CreateObject("WScript.Shell")
      		objShell.run ("Explorer" &" " & strDirectory & "\" )
    'Create the Open dialog, but pause 500 ms so it gets focus
      WScript.Sleep 500
      Set objDLG = CreateObject("UserAccounts.CommonDialog")
      		objDLG.Filter = "PC-DMIS Programs|*.PRG|All files|*.*"
      		objDLG.FilterIndex = 1
      		objDLG.InitialDir = strDirectory
    'Show the Open dialog
      		objDLG.ShowOpen
    'If a program were chosen in the Open dialog, open it in PC-DMIS
      		If objDLG.FileName <> "" Then
      		  Set PCDApp = CreateObject("PCDLRN.Application")
    		  Set PCDPartPrograms = PCDApp.PartPrograms
      		  PCDPartPrograms.Open objDLG.FileName, "CMM1"   
    		  MsgBox "Program loaded in PC-DMIS.", vbInformation, "Info"
      		End If
    'Cleanup
      		Set PCDPartPrograms = nothing
      		Set PCDApp = nothing
    End If
    'Finish
    WScript.Quit
    Example:

    My part is name 12345, so I create a folder in the '\\server_path\1-' folder named '12345'. I save my program in this folder.
    When running the script, I enter the programname (12345) and the script searches '\\server_path' for the '1-' folder
    (as the partname I entered begins with a '1'). It then opens the '\\server_path\1-' folder and looks for '12345'.
    If found it will display an open dialog, listing all .PRG's found in that directory. Additionally, Windows Explorer will open
    the folder to display the files in it, good if you have setup instructions located there too.

    As always, tweak it to suit your needs.
    Last edited by vpt.se; 10-02-2012 at 08:56 AM. Reason: EDIT: Updated code
    PC-DMIS CAD++ 2o14 SP1

  2. #2
    PC-DMIS Guru
    • Additional Information
      • Years CMM Experience:
      • Since 1994
      • Software Version:
      • PC-DMIS CAD++ 2013 MR1 SP2
      • Machine Type:
      • CEJ12C, CEJ12000
      • Location:
      • Sweden
      • Job Function:
      • Quality tech., CMM programmer/operator
    vpt.se's Avatar
    Join Date
    Feb 2007
    Location
    Sweden
    Posts
    4,770

    Default

    Made a C# version of this (for us) very valuable script. Do note that my knowledge in C# is insanely noobish and the code can be optimized and whatnot. I provide it here for others to learn from.

    Code:
    using System;
    using System.IO;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.Drawing;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Reflection; // needed to connect
    
    namespace SearchPCDMISprograms
    {    
        public partial class Form1 : Form
        {
            string serverpath, readystr, initstr;
            bool PCDStarted = false;
            dynamic PCDApp;
            public Form1()
            {
                InitializeComponent();
                serverpath = Properties.Settings.Default.serverpath; // read the serverpath from the settings file
                readystr = Properties.Settings.Default.readystr; // read the readystr from the settings file
                initstr = Properties.Settings.Default.initstr; // read the initstr from the settings file
            }
    
            private void Cancelbtn_Click(object sender, EventArgs e)
            {
                System.Windows.Forms.Application.Exit(); // closes the application
            }
    
            private void Okbtn_Click(object sender, EventArgs e)
            {
                if (textBox1.Text.Length < 1)
                {
                    MessageBox.Show("Enter a programname!");
                }
                else
                {
                    string firstchar = textBox1.Text.Substring(0, 1); // get the first char in the entered string
                    string folder = textBox1.Text; // set the folder to search for
                    string totalpath = serverpath + "\\" + firstchar + "-\\" + folder; // complete path
    
                    if (Directory.Exists(totalpath))
                    {
                        Process.Start(@totalpath); // open the folder in Explorer
    
                        OpenFileDialog openfiledialog1 = new OpenFileDialog(); // create an open file dialog
                        openfiledialog1.InitialDirectory = totalpath; // show the folder in the open file dialog
                        openfiledialog1.Filter = "prg files (*.prg)|*.prg|All files (*.*)|*.*"; // filtermasks
                        openfiledialog1.FilterIndex = 1; // initial filterindex
          
                        if (openfiledialog1.ShowDialog() == DialogResult.OK)
                        {
                            try
                            {
                                Type pcdObjType = Type.GetTypeFromProgID("PCDLRN.Application");
                                PCDApp = Activator.CreateInstance(pcdObjType);
                                string status = "";
                                /*
                                    this while loop starts with status = "", and will query PC-DMIS for the text in the status bar at the bottom of the screen.
                                    when it finally says "READY" or it contains "DONE INIT", the loop will exit - NOTE (vpt.se) that these strings must be localized
                                 */
                                while (status != readystr && !status.Contains(initstr))
                                {
                                    System.Threading.Thread.Sleep(1000); // give pc-dmis a second to work
                                    status = PCDApp.StatusBar();
                                }
                                PCDStarted = true; //PC-DMIS started
                                
                                // If PC-DMIS registered correctly in the Running Object Table, the code below
                                // (three lines) should work for connecting to an already running instance
                                //
                                //PCDApp = (PCDLRN.Application)Marshal.GetActiveObject("PCDLRN.Application");// as PCDLRN.Application;  
                                //PCDApp.PartPrograms.Open(openfiledialog1.FileName, "CMM1");
                                //Marshal.FinalReleaseComObject(PCDApp);
                            }
                            catch
                            //catch (System.Runtime.InteropServices.COMException f)
                            {
                                PCDStarted = false; // PC-DMIS did not start
                                //MessageBox.Show(string.Format("PC-DMIS must be started: {0}", f.Message)); // PC-DMIS needs to be started! 
                                //System.Windows.Forms.Application.Exit(); // Exit
                            }
                        
                        }
                        if (PCDStarted)
                        {
                            PCDApp.PartPrograms.Open(openfiledialog1.FileName, "CMM1"); // "CMM1" for online, "OFFLINE" for offline    
                            PCDApp = null;
                            Application.Exit();
                        }
                        else
                        {
                            MessageBox.Show("Couldn't start PC-DMIS!");
                            Application.Exit();
                        }
                    }
                    else
                    {
                        MessageBox.Show("Folder not found!"); // couldn't find the folder
                    }
                }
            }
        }
    }
    Credits to cappy6124 et al. for suggestions and input.
    Please note that the source is hardcoded for my setup so it will possibly error out, but hey - you gotta love sourcecode!
    PC-DMIS CAD++ 2o14 SP1

Similar Threads

  1. File open dialog box?
    By DmisDave in forum PC-DMIS for CMMs
    Replies: 3
    Last Post: 10-05-2011, 01:27 PM
  2. PC-DMIS 2010 MR2 - GD&T dimensions Dialog Box
    By Milorad in forum PC-DMIS for CMMs
    Replies: 4
    Last Post: 05-21-2011, 12:54 PM
  3. "OPEN" dialog box
    By hobbie in forum PC-DMIS for CMMs
    Replies: 13
    Last Post: 04-24-2009, 12:38 PM
  4. Can't open a program
    By Carl Winsett in forum PC-DMIS for CMMs
    Replies: 14
    Last Post: 10-03-2007, 10:43 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •