0 Replies Latest reply on Apr 21, 2017 12:32 PM by Sean Donnelly

    Popup Card Issue C# Menu Command - Card won't save (previous post included)

    Sean Donnelly

      This is a followup to my post from yesterday. That post has many responses and I wanted to start clear. Thanks to Jim's help, I have the datacard properly showing in the popup! However, I can't seem to get this card to save when a change is made. My assumption is maybe it's where the "cast" takes place in the CreateCardView method. This is the final piece for this project. I'll ten look at cleaning all of this up and make it better, but this is the last technical piece.

       

      The updated code with the previous solution is below. Thanks for all of the help, I'm sorry if this is inconveniencing anyone, but it's very helpful and I've learned a lot from here.

       

      Previous post:

      Pop Up DataCard when file created (similar to template option) C#

       

      using System;
      using System.IO;
      using System.Collections.Generic;
      //using System.Linq;
      using System.Text;
      //using System.Threading.Tasks;
      using EdmLib;
      using System.Drawing;
      using Microsoft.VisualBasic;
      using System.Windows.Forms;
      namespace ClassLibrary2
      {
          public class Class1 : IEdmAddIn5, EdmCallback
          {
              public void GetAddInInfo(ref EdmAddInInfo poInfo, IEdmVault5 poVault, IEdmCmdMgr5 poCmdMgr)
              {
                  //Specify information to display in the add-in's Properties dialog box   
                  poInfo.mbsAddInName = "Menu command sample";
                  poInfo.mbsCompany = "SOLIDWORKS Corporation";
                  poInfo.mbsDescription = "Adds menu command items";
                  poInfo.mlAddInVersion = 1;
                  //Specify the minimum required version of SOLIDWORKS Enterprise PDM  
                  poInfo.mlRequiredVersionMajor = 10;
                  poInfo.mlRequiredVersionMinor = 0;
                  //Register a menu command  
                  poCmdMgr.AddCmd(100, "Inspection Request (New C#)", (int)EdmMenuFlags.EdmMenu_OnlySingleSelection);
              }
      
      
              IEdmCardView63 view;
      
      
              public void OnCmd(ref EdmCmd poCmd, ref Array ppoData)
              {
                  if (poCmd.meCmdType == EdmCmdType.EdmCmd_Menu) //This makes sure the addin was called on a menu right click  
                  {
                      if (poCmd.mlCmdID == 100) //This makes sure the menu command ID was 100, which means your addin was the one called  
                      {
                          //Let's get moving, it's our turn!  
                          IEdmVault12 myVault = (IEdmVault12)poCmd.mpoVault; //Cast the vault to an Iedmvault12 object for latest functionality  
                          string Mymessage = null;//instantiate a string message  
                          //string StrID = null;//instantiate the ID of the object  
                          if ((((EdmCmdData)ppoData.GetValue(0)).mlObjectID1) != 0) //mlObjectID1 will be 0 if a folder was selected and the ID of the file if a file was selected during a menu command  
                          {
                              //Mymessage = Mymessage + "File: (ID=";
                              IEdmFile5 myfile = (IEdmFile10)myVault.GetObject(EdmObjectType.EdmObject_File, (((EdmCmdData)ppoData.GetValue(0)).mlObjectID1));//Get the file object  
      
      
                              string fileNoExt = Path.GetFileNameWithoutExtension(myfile.Name);
                              
                              IEdmEnumeratorVariable10 myvars = default(IEdmEnumeratorVariable10); //Set the enumeratorvariable object  
                              myvars = (IEdmEnumeratorVariable10)myfile.GetEnumeratorVariable(); //Cast the enumeratorvariable object to the file  
                              object CreatedBy = null; //Create a variable to hold the model no.  
                              object Desc1 = null;
                              object Desc2 = null;
                              object Rev = null;
      
      
                              myvars.GetVar("CreatedBy", "@", out CreatedBy); //Use the GetVar method  
                              myvars.GetVar("CAD_Description", "@", out Desc1);
                              myvars.GetVar("CAD_Description1", "@", out Desc2);
                              myvars.GetVar("CAD_Revision", "@", out Rev);
      
      
                              myvars.CloseFile(false); //Always close the file when done accessing card variables to avoid runtime errors  
                                                       //Mymessage = Mymessage + "File ID: " + myfile.ID + System.Environment.NewLine; //Create a message with the File ID  
      
      
                              Mymessage = Mymessage + "CreatedBy: " + CreatedBy + System.Environment.NewLine; //Add the model no. variable  
                              Mymessage = Mymessage + "Description: " + Desc1 + System.Environment.NewLine;
                              Mymessage = Mymessage + "Description1: " + Desc2 + System.Environment.NewLine;
                              Mymessage = Mymessage + "Revision: " + Rev + System.Environment.NewLine;
      
      
                              MessageBox.Show(Mymessage); //Show the message box!  
                              
                              string templPath = @"C:\SeanVault\Templates\template.item.cvd";
                              string newPath = @"C:\SeanVault\Inspection Files";
                              string filename = fileNoExt + " Rev " + Rev.ToString() + ".insp";
                              IEdmFile5 templateFile = default(IEdmFile5);
                              IEdmFolder5 srcFolder = null;
                              templateFile = myVault.GetFileFromPath(templPath, out srcFolder);
                              
                              IEdmFolder5 destFolder = default(IEdmFolder5);
                              destFolder = myVault.GetFolderFromPath(newPath);
                              if (destFolder == null)
                                  return;
      
      
      
      
                              int fileID = 0;
                              fileID = destFolder.CopyFile(templateFile.ID, srcFolder.ID, 0, filename, (int)EdmCopyFlag.EdmCpy_Simple);
                              
                              Interaction.MsgBox("Copied file successfully to new file with ID, " + fileID);
      
      
                                                                              
                              IEdmFile5 newFile = default(IEdmFile5);
                              IEdmFile5 itemFile = default(IEdmFile5);
      
      
                              newPath = newPath + "\\" + filename;
      
      
                              //Search for file with .item extension
                              IEdmSearch5 Search = default(IEdmSearch5);
                              Search = (IEdmSearch5)myVault.CreateUtility(EdmUtility.EdmUtil_Search);
      
      
                              Search.FindFiles = true;
                              //Search.StartFolderID = 102; //Regular Items ID
                              Search.FileName = fileNoExt + ".item";
                              IEdmSearchResult5 Result = default(IEdmSearchResult5);
                              
      
      
                              Result = Search.GetFirstResult();
                              object StockingType = null; //Create a variable to hold the model no. 
                              if (Result != null)
                              {
                                  MessageBox.Show("Result exists. Begin! Path = " + Result.Path);
                                  itemFile = myVault.GetFileFromPath(Result.Path, out srcFolder);
                                  IEdmEnumeratorVariable10 itemVars = default(IEdmEnumeratorVariable10); //Set the enumeratorvariable object  
                                  itemVars = (IEdmEnumeratorVariable10)itemFile.GetEnumeratorVariable(); //Cast the enumeratorvariable object to the file  
                                  itemVars.GetVar("ITEM_StockingType", "", out StockingType); //Use the GetVar method
      
      
                                  myvars.CloseFile(false); //Always close the file when done accessing card variables to avoid runtime errors  
                                                           //Mymessage = Mymessage + "File ID: " + myfile.ID + System.Environment.NewLine; //Create a message with the File ID  
                              }
      
      
                              newFile = myVault.GetFileFromPath(newPath, out srcFolder);
                              //update variables
                              IEdmEnumeratorVariable10 myNewVars = default(IEdmEnumeratorVariable10); //Set the enumeratorvariable object  
                              myNewVars = (IEdmEnumeratorVariable10)newFile.GetEnumeratorVariable(); //Cast the enumeratorvariable object to the file  
                              
                              myNewVars.SetVar("ITEM_RevisionNumber", "", Rev.ToString());
                              myNewVars.SetVar("ITEM_DescriptionLine1", "", Desc1.ToString());
                              myNewVars.SetVar("ITEM_DescriptionLine2", "", Desc2.ToString());
      
      
                              if (StockingType.ToString() != null)
                              {
                                  myNewVars.SetVar("ITEM_StockingType", "", StockingType.ToString());
                              }
      
      
                              myvars.CloseFile(false); //Always close the file when done accessing card variables to avoid runtime errors  
                                                       //Mymessage = Mymessage + "File ID: " + myfile.ID + System.Environment.NewLine; //Create a message with the File ID  
      
      
                              // Create a new instance of the form.
                              Form form1 = new Form();
                              // Create two buttons to use as the accept and cancel buttons.
                              Button button1 = new Button();
                              Button button2 = new Button();
      
      
                              // Create the card view interface 
                              view = (IEdmCardView63)destFolder.CreateCardView(fileID, (int)form1.Handle, 40, 300, (EdmCallback)this);
                              if (view == null)
                              {
                                  Interaction.MsgBox("The file does not have a card.");
                                  return;
                              }
      
      
                              // Set input focus to the first control in the card
                              view.SetFocus(0);
      
      
                              // Enable all controls in the card
                              view.Update(EdmCardViewUpdateType.EdmCvut_EnableCtrl);
      
      
                              // Get the size needed to display the card 
                              int width = 0;
                              int height = 0;
                              view.GetCardSize(out width, out height);
      
      
                              // Resize the form window to make room for the card 
                              form1.Width = (width + 100);
                              form1.Height = (height + 400);
                              view.ShowWindow(true);
      
      
                              //SaveCard.Enabled = false;
      
      
      
      
                              // Set the text of button1 to "OK".
                              button1.Text = "OK";
                              // Set the position of the button on the form.
                              button1.Location = new Point(10, 10);
                              // Set the text of button2 to "Cancel".
                              button2.Text = "Cancel";
                              // Set the position of the button based on the location of button1.
                              button2.Location
                                 = new Point(button1.Left, button1.Height + button1.Top + 10);
                              // Make button1's dialog result OK.
                              button1.DialogResult = DialogResult.OK;
                              // Make button2's dialog result Cancel.
                              button2.DialogResult = DialogResult.Cancel;
                              // Set the caption bar text of the form.   
                              form1.Text = "My Dialog Box";
      
      
                              // Define the border style of the form to a dialog box.
                              form1.FormBorderStyle = FormBorderStyle.FixedDialog;
                              // Set the accept button of the form to button1.
                              form1.AcceptButton = button1;
                              // Set the cancel button of the form to button2.
                              form1.CancelButton = button2;
                              // Set the start position of the form to the center of the screen.
                              form1.StartPosition = FormStartPosition.CenterScreen;
      
      
                              // Add button1 to the form.
                              form1.Controls.Add(button1);
                              // Add button2 to the form.
                              form1.Controls.Add(button2);
      
      
                              // Display the form as a modal dialog box.
                              form1.ShowDialog();
      
      
                              // Determine if the OK button was clicked on the dialog box.
                              if (form1.DialogResult == DialogResult.OK)
                              {
                                  // Display a message box indicating that the OK button was clicked.
                                  MessageBox.Show("The OK button on the form was clicked.");
      
      
                                  //BREAKS HERE
                                  view.SaveData();
                                  
                                  MessageBox.Show("Save Complete");
      
      
                                  // Optional: Call the Dispose method when you are finished with the dialog box.
                                  form1.Dispose();
                              }
                              else
                              {
                                  // Display a message box indicating that the Cancel button was clicked.
                                  MessageBox.Show("The Cancel button on the form was clicked.");
                                  // Optional: Call the Dispose method when you are finished with the dialog box.
                                  form1.Dispose();
                              }
      
      
                              //Check-in new file
                              newFile.UnlockFile(0,"Created and checked-in with Sean's API.");
      
      
                              //myvars.SetVar("CAD_Weight","@", "This Worked");
      
      
                              //Mymessage = Mymessage + "File name: " + ((EdmCmdData)ppoData.GetValue(0)).mbsStrData1 + System.Environment.NewLine; //Add the file name  
      
      
                          }
                      }
                  }
              }
              public void SetModifiedFlag()
              {
                  throw new NotImplementedException();
              }
      
      
      
      
              public void SetProgressPos(int lPos)
              {
                  throw new NotImplementedException();
              }
      
      
      
      
              public void SetProgressRange(int lMin, int lMax)
              {
                  throw new NotImplementedException();
              }
      
      
      
      
              public void SetStatusMessage(string bsMessage)
              {
                  throw new NotImplementedException();
              }
          }
      }