Deleting entities from a sketch

Hi,

I am trying to create then delete a line in a sketch.  For testing purposes, I create two lines, and delete the first one.  Strange thing is, the following code results in the second line being deleted, even though the first one was explicitely selected.

                SketchManager skm = SWIntegration.Instance.getModel().SketchManager;

                skm.AddToDB = true;

 

                SketchSegment ss = skm.CreateLine(0, 0, 0, 1, 1, 0);

                ss.ConstructionGeometry = true;

               

                SketchSegment ss2 = skm.CreateLine(0, 0, 0, 1, 2, 0);

                ss2.ConstructionGeometry = true;               

                SelectData sd = SWIntegration.Instance.getSelectData();

                ss.Select4(false, sd);

                SWIntegration.Instance.getModel().EditDelete();

                skm.AddToDB = false;

I also tried clearing the selection before selecting the line to be deleted, like so:

                [...]

                SWIntegration.Instance.getModel().ClearSelection2(true);

                SelectData sd = SWIntegration.Instance.getSelectData();

                ss.Select4(true, sd);

                SWIntegration.Instance.getModel().EditDelete();

                [...]

... but then I see a message a the top right corner of the screen saying that "entities which do not belong to the active sketch cannot be deleted".  How can they not be in the active sketch?  I don't understand.

Thanks in advance,

Louis

SolidworksApi macros
Answers 1
SF
SF 2013-11-15 Best Answer

Solved my problem on my own.  I also tried selecting the sketch entities with SelectByID2, because it advised to use that instead of the Select4 methods in ISketchSegment, ISketchPoint, etc... but I got an exception when I passed 'null' as the callout argument... I don't know why, looks like a bug to me.  But I looked again at it's documentation, and I saw at the bottom that I could use

ISelectionMgr::AddSelectionListObjects instead.

I really wish that information would have been also in the Select4 method's documentation page, that would have saved me a lot of head scratching.  What use is the Select4 method then, if it just doesn't work and the documentation advises NOT to use it, but not in it's own doc page?  sigh...

anyhoo, here is the fix, for anyone struggling with similar problems:

        

                SketchManager skm = SWIntegration.Instance.getModel().SketchManager;

                skm.AddToDB = true;

                SketchSegment ss = skm.CreateLine(0, 0, 0, 1, 1, 0);

                SketchSegment ss2 = skm.CreateLine(0, 0, 0, 1, 2, 0);

                SelectData sd = SWIntegration.Instance.getSelectData();

                SelectionMgr sm = SWIntegration.Instance.getModel().SelectionManager;

                sm.SuspendSelectionList();

                DispatchWrapper[] dp = new DispatchWrapper[1];

                dp[0] = new DispatchWrapper(ss);

                sm.AddSelectionListObjects(dp, sd);

                SWIntegration.Instance.getModel().EditDelete();

                sm.ResumeSelectionList();

               

there you go.