I tried to isolate the problem. Apparently the error is related to the Insert3DSketch function. I can enter into sketch mode, insert a sketch, but as soon as I try to use Insert3DSketch, Solidworks crashes.
Please answer some questions below which may help to understand the problem?
- Are you in Part or in Assembly?
- Why do you call Sketch3DIntersection second time after clear selection (so no entities are selected required for this method)?
- How to did you retrieve the pointers to bo and myReferencePlane?
Hi Artem, thanks for your reply.
- I'm in Part
- Sorry, it was a leftover from the macro I tried to imitate after several unsuccessful tries. Actually I call Sketch3DIntersection, ClearSelection2 and InsertSketch.
- bo is actually retrieved from an edge using Edge.GetBody(). myReferencePlane is a RefPlane I made using the very same edge as constraint. If I stop the execution before Insert3DSketch, I can see that they are correctly selected in SW.
That's very strange. I don't think that the problem is due to bo/myReferencePlan, simply calling Insert3DSketch, even before everything else, leads to a crash.
Hi, any update on this? Thank you.
Is your macro just does an intersection and no more actions? If so could you please attach this macro and the model? Otherwise
try to create a very simple macro which just does 3D intersection. Does it crashes? if not you need to look in your previous code and try to suppress the block to find what is causing it. I believe the problem is not a 3D Sketch inserting, even not 3D intersection but something which happened before.
In most cases one of the following may cause such a behavior:
- Incorrect use of temp bodies. For example some of the APIs are intended to be used for temp bodies while used on regular ones or vice-versa (for example Body2::Hide for temp body while Body2::HideBody for regular ones)
- Incorrect use of contexts (for example you are trying to insert a feature into a Part from assembly without putting the component into edit mode
The macro is actually really simple. The user selects an edge and press the macro's button that:
- Inserts a reference plane using the selected edge as constraint and stores it in a variable
- Clears the selection
- Selects both the body (using Edge.GetBody) and the reference plane
- Creates the 3D intersection
I tried to execute the macro until the third point and then run another macro that only contains Insert3DSketch. Strangely, it works fine.
Could it be due to the fact that I'm executing the entire code of the first macro in the OnClose of a PMP?
Thank you for your time.
Yes, I believe this is the reason. You should not do any rebuild operations or create new feature while PMP is open (it is not closed in OnClose event). You have two ways to fix this
- Make PMP locked by using swPropertyManagerOptions_LockedPage while creating
- Move your operation into AftreClose event (this is preferable solution). Please note that this event fired when the PMP is closed so you can no longer access the values from the controls. So you need to store all the data (selections, inputs) from within OnClose event and use them to create an intersection in AfterClose event.
Thanks, moving the code into AfterClose fixed the problem.