4 Replies Latest reply on Dec 3, 2018 4:01 PM by Peter Brinkhuis

    How to get the bounding box for block in drawing

    Paine Zeng

      Hello everyone,

           I want insert some blocks use my tool, but when i insert the first one, i can't get the block size (width and height). i want insert the next block close to the first one( No Overlap).

           so i want to get the bounding box like the part or assembly. but i can't find the API . who can help me to fine a solution?

        • Re: How to get the bounding box for block in drawing
          Matt Bieringer

          You can use the ISketchBlockDefinition.GetSketch and set the sketch to a feature and get the bounding box that way. It should look something like this:

           

          Dim swFeature As Feature
          Dim swSketch As Sketch
          Dim BBox As Variant
          Dim bRet as Boolean
          
          Code where you create your block
          
          Set swSketch = swSketchBlockDef.GetSketch
          Set swFeature = swSketch
          bRet = swFeature.GetBox(BBox)
          Debug.Print BBox(0)
          Debug.Print BBox(1)
          Debug.Print BBox(2)
          
            • Re: How to get the bounding box for block in drawing
              Paine Zeng

              yes, now i can get the bounding box size. thanks.

              • Re: How to get the bounding box for block in drawing
                Peter Brinkhuis

                I just tried this and it didn't work for me. It only works on the sketch block instance, not the definition. That might be because my block has properties in it that are determined by the view it is linked to, so the block instance size can vary while the definition is the same. I'm testing this is SW2017.

                 

                I like the creativity on getting to the bounding box by the way, I just wish SolidWorks would have a more consistent way of getting the size of objects. For notes you have to use note.GetExtent(), how do you find that out?

                • Re: How to get the bounding box for block in drawing
                  Peter Brinkhuis

                  Update:

                  1. Getting the instance size: swBlockInstance > GetSketch() > as Feature > GetBox() indeed only work on smart blocks, that is blocks with text that is linked to the view. This makes sense, because each block's size can be different.
                  2. If the block is dumb, get the definition as Matt proposed: swBlockInstance > Definition > GetSketch() > as Feature > GetBox()
                  3. If you have multiple blocks within a single view swBlockInstance.GetSketch returns the single sketch that contains all blocks. The size of the sketch feature is therefore the bounding box around all blocks. This is not what you need, not what I needed at least.

                   

                  I have still found no way to get the size of each smart block when a view has multiple blocks. And I could really use that. Anybody has any idea how to get the size of the block instance?

                   

                  I tried casting it to an Annotation, to a Note, I tried getting all arcs and text from the block. Still no dice. I have created such a nice piece of software for adding blocks and notes, only to find out that it only works when you add a single block