Search code examples
solidworks

How can get Note's position which in SketchBlock(SolidWorks)


I trying to get text and position of all Notes from draw. I could that except what be in block.

When i get Notes from block, To get text is successful But coordinates are weird.

      SketchManager swSketch = swModel.SketchManager;
      var blocks = swSketch.GetSketchBlockDefinitions();
      for (int z = 0; z < blocks.Length; z++)
      {
           SketchBlockDefinition block = blocks[z];
           var swNotes = block.GetNotes();

           for (int x = 0; x < swNotes.Length; x++)
           {
               PosX = "";
               PosY = "";
               displayText = "";
               Note swBlockNote = swNotes[x];

               swAnn = swBlockNote.GetAnnotation();
               swDispData = swAnn.GetDisplayData();

               dPoint = swDispData.GetTextPositionAtIndex(0);
               dPoint[0] = dPoint[0] * 1000;
               dPoint[1] = dPoint[1] * 1000;
               PosX_num = dPoint[0].ToString("########0.#########");
               PosY_num = dPoint[1].ToString("########0.#########");
           }
      }

enter image description here

That triangle is block and have note text "A123" also have position 72.29mm, 227.559mm

But The triangle's position is X:0.002375 Y:-0.02656 When i try getting in program.

Can i get some idea, Waiting your help sincerely. Thank you.


Solution

  • BlockDefinitions is just definition. I had to get BlockInstances like this.

    Sketch sktch = swView.GetSketch();
    var blockInstance = sktch.GetSketchBlockInstances();
    

    Then i could get Block's position. But we should be careful that block position which get from block instance based view's position.

    for example, if you range a block to view which have position 0, 0 you can get right block position just try to get position from blockinstance.

    But if you range a block to view which have position, you must sum view's position to blocinstance's position. Also, Note's Positions are same. Notes in block are have position based BlcokDefinition.

    Just refer this sample code because i can't explain well with English.

    Sketch sktch = swView.GetSketch();
    var blockInstance = sktch.GetSketchBlockInstances();
    if (blockInstance != null)
    {
        double[] blockPos = null;
        double[] viewPos = null;
        for (int instanceCnt = 0; instanceCnt < blockInstance.Length; instanceCnt++)
        {
             SketchBlockInstance instance = blockInstance[instanceCnt];
             SketchBlockDefinition block = instance.Definition;
             MathPoint InstanceMP = instance.InstancePosition;
             blockPos = InstanceMP.ArrayData;
             viewPos = swView.Position;
             var swNotes = block.GetNotes();
             for (int x = 0; x < swNotes.Length; x++)
             {
                  Note swBlockNote = swNotes[x];
    
                  swAnn = swBlockNote.GetAnnotation();
                  swDispData = swAnn.GetDisplayData();
                  dPoint = swDispData.GetTextPositionAtIndex(0);
                  dPoint[0] = (dPoint[0] * 1000) + (blockPos[0] * 1000) + (viewPos[0] * 1000);
                  dPoint[1] = (dPoint[1] * 1000) + (blockPos[1] * 1000) + (viewPos[1] * 1000);
             }
         }
    }