dxf-parser icon indicating copy to clipboard operation
dxf-parser copied to clipboard

MLeader: fix text height

Open camnewnham opened this issue 3 years ago • 0 comments

The ADSK spec shows two values for textHeight; I (wrongly) assumed a dxf writer would use one or the other. This keeps both values. Good chance I'll run into some more of these over time as well...

Also updated readme.md to reflect MLeader support.

Not directly related, but here's a sample of drawing an MText based MLeader in a fashion that's similar to three-dxf.

threejs sample code

  function drawMLeader(entity: ILeaderEntity, data: any) {
    const group = new Object3D();

    const color = getColor(entity, data);

    const context = entity.contextData;

    if (context.hasMText) {
      const textMat = getMaterialForColour(color, "basic");

      const geometry = new TextGeometry(context.defaultTextContents, {
        font: font,
        height: 0,
        size: context.textHeight ?? context.textHeightAlt ?? 12,
      });

      const offset = new Vector3(
        0,
        -(context.textHeight ?? context.textHeightAlt ?? 12)
      );

      if (context.landingGap) {
        if (context.textDirection) {
        }
      }

      offset.x += context.landingGap ?? 0;

      geometry.translate(offset.x, offset.y, 0);

      const text = new THREE.Mesh(geometry, textMat);

      if (context.textLocation) {
        // Justifications already applied
        text.position.set(
          context.textLocation.x,
          context.textLocation.y,
          context.textLocation.z ?? 0
        );
      } else if (context.contentBasePosition) {
        // TODO Apply justifications
        text.position.set(
          context.contentBasePosition.x,
          context.contentBasePosition.y,
          context.contentBasePosition.z ?? 0
        );
      }

      group.add(text);
    } else if (context.hasBlock) {
      console.warn("Block leaders are not yet implemented");
      return null;
    }

    const lineMat = getMaterialForColour(color, "line");
    context.leaders.forEach((leader) => {
      leader.leaderLines.forEach((line) => {
        line.vertices.forEach((vArr) => {
          const arrLength = leader.hasSetLastLeaderLinePoint
            ? vArr.length + 1
            : vArr.length;

          const attr = new BufferAttribute(
            new Float32Array(arrLength * 3),
            3,
            false
          );
          vArr.forEach((v, i) => {
            attr.setXYZ(i, v.x, v.y, v.z);
          });
          if (leader.hasSetLastLeaderLinePoint) {
            attr.setXYZ(
              arrLength - 1,
              leader.lastLeaderLinePoint.x,
              leader.lastLeaderLinePoint.y,
              leader.lastLeaderLinePoint.z
            );
          }
          const buffer = new BufferGeometry();
          buffer.setAttribute("position", attr);
          var ls = new Line(buffer, lineMat);
          group.add(ls);
        });
      });
    });

    return group;
  }

camnewnham avatar Jun 08 '22 07:06 camnewnham