-
Re: Event active doc is an assy
Simon Turner Dec 23, 2015 3:59 AM (in response to Yannick Proulx)If you used the Visual Studio template from the API SDK, you should have a function like this (in the main addin class):
Function SldWorks_ActiveModelDocChangeNotify() As Integer
'TODO: Add your implementation here
End Function
Replace the TODO line with your code to check what the active document is, and enable/disable your control.
-
Re: Event active doc is an assy
Amen Allah Jlili Dec 23, 2015 5:28 AM (in response to Yannick Proulx)Expanding on Simon Turner's answer:
SolidWorks provide event handlers (in forms of delegates if you're using C#) for managing events. The following event handler contained in SldWorks class should execute when the active model is changed. Please note this will not work for parts editing from inside an assembly.
Declaration:
public delegate System.int DSldWorksEvents_ActiveModelDocChangeNotifyEventHandler()
Usage:
Simply add the method that is going to handle the event to the event handler to your add-in sldworks object:
// Assume that swApp is already instanciated swApp.ActiveModelDocChangeNotify += this.MyMethod() public int MyMethod() { ModelDoc2 swModelDoc = (ModelDoc2)swApp.ActiveDoc; if (swModelDoc != null) { return 0; } if (swModelDoc.GetType == (int)swDocumentTypes_e.swDocASSEMBLY) { // Do stuff here: enable the button return 1; } else { // Do stuff here: disable the button return 0; } }
and VOILA! SolidWorks executera cette méthode à chaque activation d'un nouveau modele!
-
Re: Event active doc is an assy
Yannick Proulx Dec 23, 2015 7:45 AM (in response to Amen Allah Jlili)Hi Guys,
I tried to convert this c# code to Vb.net and I got a little problem.
even if I declare the ActiveModelDocNotify, I'm not able to write the swapp.ActiveModelDocChangeNotify = me.Bom_Compare_button
the intelisense doesn't give me the choose of ActiveModelDocChangeNotify. Any Idea why?
(another question, How did you had code snippet with line number like you did...it's way more clear like that?)
here is my conversion result:
Public Delegate Function DSldWorksEvents_ActiveModelDocChangeNotifyEventHandler() As System.Int32
' Public Properties
ReadOnly Property SwApp() As SldWorks.SldWorks
Get
Return iSwApp
End Get
End Property
'the problem is down here
Function SldWorks_ActiveModelDocChangeNotify() As Integer
SwApp.--------------------------------------------------------
End Function
Public Function Bom_Compare_button() As Integer
Dim swModelDoc As ModelDoc2 = DirectCast(SwApp.ActiveDoc, ModelDoc2)
If swModelDoc IsNot Nothing Then
Return 0
End If
If swModelDoc.[GetType] = CInt(swDocumentTypes_e.swDocASSEMBLY) Then
MsgBox("file is an assy")
Return 1
Else
MsgBox("file is NOT an assy")
Return 0
End If
End Function
Thanks again for your time
-
Re: Event active doc is an assy
Amen Allah Jlili Dec 23, 2015 8:30 AM (in response to Yannick Proulx)You don't declare the delegate! It is a member of the instantiated the SldWorks object. Look at my IDE
Remove this line:
Public Delegate Function DSldWorksEvents_ActiveModelDocChangeNotifyEventHandler() As System.Int32You're calling on the property not the object. You should declare a sldworks object, give it a value using the property swapp() and then using that object and add the event handling method to the event handler of that object! C'est aussi simple que ça!
-
Re: Event active doc is an assy
Simon Turner Dec 23, 2015 8:56 AM (in response to Yannick Proulx)Is your iSwapp object declared WithEvents? I think it should be.
If so, add this line into your ConnectToSW function:
AddHandler iSwApp.ActiveModelDocChangeNotify, AddressOf Me.SldWorks_ActiveModelDocChangeNotify
Then it will call SldWorks_ActiveModelDocChangeNotify every time the active document is changed in SolidWorks.
You can either call Bom_Compare_button from within SldWorks_ActiveModelDocChangeNotify, or you could just move the code from within Bom_Compare_button to SldWorks_ActiveModelDocChangeNotify and get rid of the Bom_Compare_button function.
EDIT: just looking at your Bom_Compare_button function, it doesn't do anything. Your original question was about activation/deactivating a button in the Task Pane.
You need to enable or disable that control yourself.
-
Re: Event active doc is an assy
Amen Allah Jlili Dec 23, 2015 9:21 AM (in response to Simon Turner)Just a side note. If the sldworks is declared with WithEvents, he can simply add handles keyword to the event handling function.
Public Sub SldWorks_ActiveModelDocChangeNotify() Handles iswapp.iSwApp.ActiveModelDocChangeNotify
without having to disturb the implementation of the connectToSW function implementation.
-
Re: Event active doc is an assy
Yannick Proulx Dec 23, 2015 10:31 AM (in response to Amen Allah Jlili)here is my code in the SwAddin module(see image below)
Simon :
i did check if my ISwapp object is declared WithEvents and it is
Amen: i did change my function line to add the handles iswapp.ActiveModelDocChangeNotify
I want to sendmsgtouser just to test the code but I still get nothing
What did I miss?
Thanks again for your help
Yannick
-
Re: Event active doc is an assy
Amen Allah Jlili Dec 23, 2015 10:40 AM (in response to Yannick Proulx)When you're debugging, can't you make set breakpoints to see if the event handler gets fired?
-
Re: Event active doc is an assy
Yannick Proulx Dec 23, 2015 10:55 AM (in response to Amen Allah Jlili)I can add one before a start my debug session then after i get the folowing message when I hover the breakpoint symbol:
“The breakpoint will not currently be hit. No symbols have been loaded for this document.”
-
Re: Event active doc is an assy
Yannick Proulx Dec 23, 2015 11:06 AM (in response to Yannick Proulx)-
Re: Event active doc is an assy
Simon Turner Dec 23, 2015 11:44 AM (in response to Yannick Proulx)I can think of 3 possibilities:
1. Your addin is not actually added in (check in the Tools -> Addins menu)
2. You are in Released mode rather than Debug mode (unlikely)
3. Some problem in your ConnectToSW function - can you show us that?
-
Re: Event active doc is an assy
Yannick Proulx Dec 23, 2015 11:53 AM (in response to Simon Turner)DAMN!!!! i was NOT in DEBUG...
sorry about this one
there was another little problem in the following line:
if SwModelDoc isNot nothing then ' in fact IsNot should be replace by Is
because it was written IsNot when solidworks got to this line with an active part open it just return 0 then nothing else instead of going to the next line
Well thanks alot Guys!!! it really help me
Yannick
-
-
-
-
-
-
-
-
-
-
Re: Event active doc is an assy
Yannick Proulx Dec 23, 2015 3:10 PM (in response to Yannick Proulx)finally I still need your help
i'm not able to access my taskpane button to set the property enable = true
I get the following message but the class SwTaskPaneHost is public so why i can't access it
I try to access the button in SwTaskPaneHose from the SwAddin Module
once again, thank you for your time
Yannick
-
Re: Event active doc is an assy
Simon Turner Dec 24, 2015 4:11 AM (in response to Yannick Proulx)SwTaskPaneHost is the name of the class. You will have created an instance of that class when you added the control to the Task Pane.
You need to use the instance.Button_Bom_Compare
Something like:
swTaskPaneView = swApp.CreateTaskpaneView2(bitmap, toolTip)
'code to add your control to the task pane view...
myTaskPaneHost = swTaskPaneView.GetControl.
'now you can access the button on the control:
myTaskPaneHost.Button_Bom_Compare.Enabled = False
-
Re: Event active doc is an assy
Yannick Proulx Jan 4, 2016 11:08 AM (in response to Simon Turner)it finally works
Thanks Alot for your time and Have a great year!
Yannick
-
-