by sylcote » Wed May 19, 2010 5:41 pm
here is the complete code. The problem seem to come from customPropertyManager. Do the solidworks dll need to be register in the assembly folder if they are in the executable folder? As for your thread question, I have no idea... I'm a mechanical engineer and I learn C# and solidworks API with your books.
Thanks for your help!!
Sylvain
P.S. I have also included the zipped source
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using SldWorks;
using SWPublished;
using SwConst;
using SwCommands;
using SolidWorksTools;
using SolidWorksTools.File;
using System.Windows.Forms;
namespace OutilsFedico
{
[Guid("4a6d7bf3-6e08-4707-a825-73de770f122c"), ComVisible(true)]
[SwAddin(Description = "Outils Fedico", Title = "Outils Fedico", LoadAtStartup = true)]
public class FedicoSwAddin : ISwAddin
{
ISldWorks iSwApp;
ICommandManager iCmdMgr;
PropFedico myProp;
#region ISwAddin Members
public bool ConnectToSW(object ThisSW, int Cookie)
{
iSwApp = (ISldWorks)ThisSW;
//iSwApp.SendMsgToUser2("Hello2", (int)swMessageBoxIcon_e.swMbInformation, (int)swMessageBoxBtn_e.swMbOk);
iSwApp.SetAddinCallbackInfo(0, this, Cookie);
iCmdMgr = iSwApp.GetCommandManager(Cookie);
//iSwApp.SendMsgToUser2("Hello3", (int)swMessageBoxIcon_e.swMbInformation, (int)swMessageBoxBtn_e.swMbOk);
AddCommandMgr();
//iSwApp.SendMsgToUser2("Hello5", (int)swMessageBoxIcon_e.swMbInformation, (int)swMessageBoxBtn_e.swMbOk);
return true;
}
public bool DisconnectFromSW()
{
//iSwApp.SendMsgToUser2("Bye",(int)swMessageBoxIcon_e.swMbInformation, (int)swMessageBoxBtn_e.swMbOk);
iSwApp = null;
GC.Collect();
return true;
}
#endregion
[ComRegisterFunctionAttribute]
public static void RegisterFunction(Type t)
{
Microsoft.Win32.RegistryKey hklm = Microsoft.Win32.Registry.LocalMachine;
Microsoft.Win32.RegistryKey hkcu = Microsoft.Win32.Registry.CurrentUser;
string keyname = "SOFTWARE\\SolidWorks\\Addins\\{" + t.GUID.ToString() + "}";
Microsoft.Win32.RegistryKey addinkey = hklm.CreateSubKey(keyname);
addinkey.SetValue(null, 0);
addinkey.SetValue("Description", "Outils Fedico");
addinkey.SetValue("Title", "Outils Fedico");
keyname = "Software\\SolidWorks\\AddInsStartup\\{" + t.GUID.ToString() + "}";
addinkey = hkcu.CreateSubKey(keyname);
addinkey.SetValue(null, 0);
}
[ComUnregisterFunctionAttribute]
public static void UnregisterFunction(Type t)
{
//Insert code here.
Microsoft.Win32.RegistryKey hklm = Microsoft.Win32.Registry.LocalMachine;
Microsoft.Win32.RegistryKey hkcu = Microsoft.Win32.Registry.CurrentUser;
string keyname = "SOFTWARE\\SolidWorks\\Addins\\{" + t.GUID.ToString() + "}";
hklm.DeleteSubKey(keyname);
keyname = "Software\\SolidWorks\\AddInsStartup\\{" + t.GUID.ToString() + "}";
hkcu.DeleteSubKey(keyname);
}
public void AddCommandMgr()
{
ICommandGroup cmdGroup;
cmdGroup = iCmdMgr.CreateCommandGroup(1, "Outils Fedico", "", "Les outils Fedico", 10);
cmdGroup.AddCommandItem2("Propriété Questica", 0, "ajouter/modifier les propriétés Questica", "", 0, "_cbPropFedico", "", 0, (int)(swCommandItemType_e.swMenuItem | swCommandItemType_e.swToolbarItem));
cmdGroup.HasToolbar = true;
cmdGroup.HasMenu = true;
cmdGroup.Activate();
}
// fonction appeler par SW pour partir le add-in
public void _cbPropFedico()
{
ModelDoc2 swModel;
Boolean PieceCommerciale = false ;
string nl = "\n";
#region Connect to Active document
swModel = (ModelDoc2)iSwApp.ActiveDoc;
if (swModel == null)
{
MessageBox.Show("Échec de la connection au document actif");
return;
}
#endregion Connect to SolidWorks
#region on vérifie si le swModel est bien un part ou assembly
if (swModel.GetType() != (int)SwConst.swDocumentTypes_e.swDocASSEMBLY && swModel.GetType() != (int)SwConst.swDocumentTypes_e.swDocPART)
{
string Message = "Les propriétés doivent être changées" + nl;
Message += "au niveau du modèle 3D ou de l'assemblage";
MessageBox.Show(Message, "Code 18.1", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
#endregion
#region on vérifie si le swModel a un nom de fichier , sinon on sort
if (swModel.GetPathName() == "")
{
MessageBox.Show("Enregistez la pièce et recommencez");
return;
}
#endregion
#region on détermine si la pièce est commercial ou fabrique
PieceCommerciale = pc(swModel);
#endregion
myProp = new PropFedico(iSwApp, PieceCommerciale);
myProp.Show();
}
public bool pc(ModelDoc2 SM)
{
CustomPropertyManager cpm;
cpm = SM.Extension.get_CustomPropertyManager("");
string theval, theval2;
string[] getnames;
getnames = (string[])cpm.GetNames();
if (getnames == null || !(getnames.Contains("IsCommercial"))) //si aucun prop ou si pas prop IsCommercial
{
DialogResult dr = MessageBox.Show("Est-ce un pièce commercial?", "Pièce commercial?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
if (dr == DialogResult.Yes)
{
return true;
}
else
{
return false;
}
}
else //prop iscommercial présente
{
cpm.Get2("IsCommercial", out theval, out theval2);
if (theval2.ToUpper() == "TRUE") // piece commercial
{
return true;
}
else //piece fab
{
return false;
}
}
}
}
}
- Attachments
-
FedicoSwAddin.rar
- (27.63 KiB) Downloaded 3 times