4 Replies Latest reply on Dec 21, 2016 4:47 AM by Eimantas Palevicius

    Delete block from only one sheet

    Eimantas Palevicius

      Hello,

       

      I am using this part of the code to delete all blocks in my selected drawing view. but when i run this code it also deletes blocks with the same name in other drawing views even in other sheets. Lets say i need to delete "block1" from "drawing view1" in "sheet2", but when i run this code it also deletes "block1" from "sheet1" "drawing view3". Any suggestions what i am doing wrong?

       

      Set swSketchMgr = swModel.SketchManager  

          vBlockDef = swSketchMgr.GetSketchBlockDefinitions

          If Not IsEmpty(vBlockDef) Then

              For i = 0 To UBound(vBlockDef)

                  Set swBlockDef = vBlockDef(i)

                  Set swFeat = swBlockDef.GetFeature

                  retval = swFeat.Select2(False, -1)

                  retval = swModelDocExt.DeleteSelection2(False)

              Next i

          End If

       

       

      Regards

      E.P.

       

      Update. Found the correct code.

        • Re: Delete block from only one sheet
          Solid Air

          Not familiar with these calls.  However, if the blocks are being deleted from the blocks folder too then most likely your are deleting them from the Feature Manager Tree and not just a single view; this will cause them to be deleted from all views.

          • Re: Delete block from only one sheet
            John Alexander

            Update. Found the correct code.

            Do you still need help with this?

             

            The actual sketch blocks that get drawn to the screen are called SketchBlockInstances. Every SketchBlockInstance has a SketchBlockDefinition (but there may be more than one instance for a definition).

            In your example, you are only looping over SketchBlockDefinitions. You need nest another loop inside that one which will access the individual SketchBlockInstance.

             

            When I've had to do something similar (delete all sketch block instances of a certain type on a specific sheet), the only way I could figure out where a SketchBlockInstance lived was through the GetSketch method. I would loop over all of the views on the current sheet, use their GetSketch method and compare the result to the SketchBlockInstance in question. If they were the same, I would delete the SketchBlockInstance.

              • Re: Delete block from only one sheet
                Eimantas Palevicius

                Yes, i used getsketchblockinstances instead of definitions and it worked.. And as long as I need to delete all the blocks from the sheet (it only has one drawing view), i was able to to so with this code:

                 

                Set swSketch = swModel.SketchManager.ActiveSketch

                    vBlockinst = swSketch.GetSketchBlockInstances

                    If Not IsEmpty(vBlockinst) Then

                        For i = 0 To UBound(vBlockinst)

                            Set swBlockInst = vBlockinst(i)

                            swBlockInst.Select False, Nothing

                            swModelDocExt.DeleteSelection2 swDeleteSelectionOptions_e.swDelete_Absorbed

                        Next

                    End If

                 

                i do not know if this is the best way, but as long as it does the work, i am fine with it.

                • Re: Delete block from only one sheet
                  Eimantas Palevicius

                  You were right. After editions in my code i found out, that my code does not work at all, or it deletes all the block definitions in drawing. So i made some adjustments regarding your tips. this is the last working version of the code:

                   

                  Set swSketch = swModel.SketchManager.ActiveSketch

                      Set swSketchMgr = swModel.SketchManager

                    

                          vblockdef = swSketchMgr.GetSketchBlockDefinitions

                          If Not IsEmpty(vblockdef) Then

                          For i = 0 To UBound(vblockdef)

                              Set swSketch = swModel.SketchManager.ActiveSketch

                              Set swBlockDef = vblockdef(i)

                              intBlockCount = swBlockDef.GetInstanceCount

                              strBlockName = swBlockDef.FileName

                              If InStrRev(strBlockName, "kornas.sldblk", -1, vbTextCompare) <> 0 Or intBlockCount = 0 Then

                                  Set swFeat = swBlockDef.GetFeature

                                  ret = swFeat.Select2(False, -1)

                                  ret = swModelDocExt.DeleteSelection2(False)

                              Else

                              End If

                          Next i

                      End If

                  The only question left - is it possible to get sketchblockdefinition name( not file name), without accessing sketchblockinstances?

                   

                  Thanks for the help, John.

                   

                  Regards

                  E.P.