How to create 3D text object?

How to create 3D text object?

traiduong014969
Collaborator Collaborator
549 Views
4 Replies
Message 1 of 5

How to create 3D text object?

traiduong014969
Collaborator
Collaborator

Hi everyone,

I have an issue that I would like to ask for expert advice on. I'm trying to create a 3D text line, but my code gets stuck at the "explode text" step. Here’s my approach:

  1. Prompt the user for the text content.
  2. Specify the insertion point.
  3. Execute the commands: TXTEXP, REGION, UNION, EXTRUDE.

Could someone please help me figure out what might be going wrong? I’m not sure why it’s not working.

Thank you!

 [CommandMethod("Create3DText")]
 public void Create3DText()
 {
    
     Document doc = Application.DocumentManager.MdiActiveDocument;
     Database db = doc.Database;
     Editor ed = doc.Editor;

     
     Application.DocumentManager.MdiActiveDocument.SendStringToExecute("_PLAN ", true, false, false);

     
     PromptStringOptions promptText = new PromptStringOptions("\nInsert your text: ");
     PromptResult textResult = ed.GetString(promptText);
     if (textResult.Status != PromptStatus.OK) return;
     string textContent = textResult.StringResult;

     
     PromptPointOptions promptPoint = new PromptPointOptions("\nSelect insert point: ");
     PromptPointResult pointResult = ed.GetPoint(promptPoint);
     if (pointResult.Status != PromptStatus.OK) return;
     Point3d textPosition = pointResult.Value;
     using (Transaction tr = db.TransactionManager.StartTransaction())
     {
        
         BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
         BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);

        
         DBText dbText = new DBText
         {
             TextString = textContent,  
             Position = textPosition,  
             Height = 10,             
         };

        
        
        
         Application.DocumentManager.MdiActiveDocument.SendStringToExecute("_TXTEXP ", true, false, false);

        
         Application.DocumentManager.MdiActiveDocument.SendStringToExecute("_REGION _ALL ", true, false, false);

         
         Application.DocumentManager.MdiActiveDocument.SendStringToExecute("_UNION ", true, false, false);

        
         Application.DocumentManager.MdiActiveDocument.SendStringToExecute("_EXTRUDE _P 5 ", true, false, false);
         btr.AppendEntity(dbText);
         tr.AddNewlyCreatedDBObject(dbText, true);

        
         tr.Commit();
     }

    
 }

 

0 Likes
550 Views
4 Replies
Replies (4)
Message 2 of 5

ActivistInvestor
Mentor
Mentor

What you're trying to do is difficult because it involves executing a mixture of built-in and LISP-based commands (TXTEXP is an express tool written entirely in LISP).

 

So, it is going to require a different approach (this will only work in an empty drawing).

 

 

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Internal;

namespace Namespace1
{
   public static class Text3DHack
   { 
      [CommandMethod("3DText")]
      public static void Create3DText()
      {
         Document doc = Application.DocumentManager.MdiActiveDocument;
         Database db = doc.Database;
         Editor ed = doc.Editor;
         ed.Command("_PLAN", "");
         PromptStringOptions promptText = new PromptStringOptions("\nInsert your text: ");
         promptText.AllowSpaces = true;
         PromptResult textResult = ed.GetString(promptText);
         if(textResult.Status != PromptStatus.OK) return;
         string textContent = textResult.StringResult;
         PromptPointOptions promptPoint = new PromptPointOptions("\nSelect insert point: ");
         PromptPointResult pointResult = ed.GetPoint(promptPoint);
         if(pointResult.Status != PromptStatus.OK) return;
         Point3d textPosition = pointResult.Value;
         using(Transaction tr = db.TransactionManager.StartTransaction())
         {
            BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
            DBText dbText = new DBText
            {
               TextString = textContent,
               Position = textPosition,
               Height = 10,
            };
            btr.AppendEntity(dbText);
            tr.AddNewlyCreatedDBObject(dbText, true);
            tr.Commit();
         }
         Utils.Do_Cmd("_TXTEXP;L;;_REGION;ALL;;_UNION;ALL;;_EXTRUDE;L;;;5", false, true, false, true);
      }
   }
}

 

0 Likes
Message 3 of 5

norman.yuan
Mentor
Mentor

As I can see, your task is a 2 stages of work: 

 

1. Identify a Text entity (either an existing one, or create a new one) and call the list-defined command "TXTEXP" (in the Express Tool) against it.

 

2. Collect the result curve entities due to the "TXTEXP" execution, then make them 3D (extruding, basically).

 

Your code obviously does not work, because the newly created Text has not been added into database, this cannot be selected (either as "Last selected", or as impliedly selected) for the command TXTEXP. Not to mention that calling SendStringToExecute() may not run at the time as you expected (it usually runs after the current code execution is done.

 

I have an article specifically about calling TXTEXP (again, it can only be called by code with SendStringToExecute(), because it is a lisp-defined command) and collecting the exploded curves. After these curves are collected, you can go ahead to make them 3D (or whatever, for that matter), pay attention to the second half of the article:

 

https://drive-cad-with-code.blogspot.com/2022/11/loop-operation-with-sendstringtoexecute.html 

 

HTH

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 4 of 5

traiduong014969
Collaborator
Collaborator

Thank you your reply. I test your code and it work. However if i want to make command in 3D view and seclect to 2 points to draw. So what should i do. I try to delete but It will not work. So could you help me for my issue?

Command("_PLAN", "");

 

traiduong014969_0-1728961070680.png

 

0 Likes
Message 5 of 5

ActivistInvestor
Mentor
Mentor

You are complicating the problem by creating the text object in the command.

 

I would suggest instead of doing that, you prompt the user to select an existing text object (which can have whatever style they want). You can copy it in-place so that the copy is the last object in the drawing, and then run the same script.

0 Likes