weird problem!?!?

All topics related to the C# programming language

weird problem!?!?

Postby sylcote » Wed May 19, 2010 2:27 pm

Hi everyone, i have a very weird problem.

I have coded an add-in for SW. it work just fine on some computer and not on some others.
Here is the piece of where is it bugging

bool PieceCommerciale = false;
MessageBox.Show("A");
PieceCommerciale = FonctionPCaaa(swModel);
MessageBox.Show("C");

private bool FonctionPCaaa(ModelDoc2 swModel)
{
MessageBox.Show("B");

CustomPropertyManager cpm;
cpm = swModel.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;
}
}

}

I have added the messagebox to pinpoint the problem.
On some computer i have messagebox A, B and then C popin up
On some other computer i only have messagebox A and nothing else. the program stop there!?!?!? I do not understand!?!?

any idea?

Thanks!
Sylvain
sylcote
 
Posts: 7
Joined: Thu Oct 30, 2008 1:08 pm

Re: weird problem!?!?

Postby angelsix » Wed May 19, 2010 2:48 pm

With only seeing a snippet of information I cannot say exactly what it is, but it is likely something that is unrelated to the lines of code, and the first thing that springs to mind is file references. Mainly SolidWorks references (with passing in a model as a parameter, the .Net engine will attempt to load the references and could crash at that point.

Also, is this on another thread or is it an STA app (single threaded)?
AngelSix.com
Luke Malpass
ICQ#: 461334120
Email: contact@angelsix.com
User avatar
angelsix
Site Admin
 
Posts: 339
Joined: Sun Mar 16, 2008 5:52 pm
Location: UK

Re: weird problem!?!?

Postby 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
sylcote
 
Posts: 7
Joined: Thu Oct 30, 2008 1:08 pm

Re: weird problem!?!?

Postby sylcote » Tue Jun 29, 2010 8:39 pm

I found the issue, .net 3.5 was not installed the computer where the addin was not crashing.

Thanks for your help!
sylcote
 
Posts: 7
Joined: Thu Oct 30, 2008 1:08 pm


Return to C# General

Who is online

Users browsing this forum: No registered users and 0 guests

cron