.NET

.NET

Reply
Distinguished Contributor
Artvegas
Posts: 104
Registered: ‎04-21-2011
Message 11 of 12 (216 Views)

Re: How to Set File Property to Layout Title Block using C#

06-02-2012 01:38 AM in reply to: vasantpadhiyar71

That's the formula you'll see in the fields dialog, but it's easiest just to use the fields dialog as noted by FFlix above. You can learn more about text fields in the AutoCAD user guide. They are extremely useful and worth knowing.

 

The easiest way I can think to describe this to you, is to run you through how you might set up a field for your custom property (without any code):-

 

1. Create a new drawing.

2. Run DWGPROPS to open the drawing properties dialog.

3. Go the the custom tab and add a custom property, for example:
    Name = ProjectName
    Value = Ground Floor Plan.

4. Exit the dialog.

5. Create a text entity using TEXT or MTEXT, and while the text cursor is still active, right-click and choose Insert Field (shortcut Ctrl +F). This will open the Field dialog.

6. In the Field category dropdown choose Document, and in the Field names list you should see your custom property name (i.e. ProjectName). Select it and then pick a Format if applicable. Click the OK button to exit the dialog.

 

You should now see your custom property value (i.e. Ground Floor Plan) in the field you created. This will be automatically updated if you change the custom property value (note though that you have to REGEN to see field changes take effect).

 

You can add fields to both text and mtext entities. And you can even add them to block attributes by right-clicking and selecting Insert Field in the relevant dialog text boxes. Where you place them all depends on how you prefer to setup your drawings.

 

If you have a title block, I would just add text or mtext with fields directly to the block rather than get into attributes, but it's really up to you.

 

Art

Contributor
vasantpadhiyar71
Posts: 23
Registered: ‎05-25-2012
Message 12 of 12 (196 Views)

Re: How to Set File Property to Layout Title Block using C#

06-03-2012 11:09 PM in reply to: Artvegas

Thanks all for your valuable Suggestion.....

 

Finally I got the Solution using Block Attributes:

 

Before editing anything Locking the Document can Solve All Issues:

 

The Code is As:

 

public class TITLEBLOCK
    {
        
        [Autodesk.AutoCAD.Runtime.CommandMethod("TITLEBLOCK1")]

        public void TitleBlock1Command(string projectname)
        {

      Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
      Database db = doc.Database;
      Editor ed = doc.Editor;
      


       string blockName = "COMPBLK";
       string attbName = "PROJECT-NAME";
       string attbValue = projectname;
       

      UpdateAttributesInDatabase(
        db,
        blockName,
        attbName,
        attbValue
        

      );
    }

    private void UpdateAttributesInDatabase(
      Database db,
      string blockName,
      string attbName,
      string attbValue
     
    )
    {
      Document doc =
        Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
      Editor ed = doc.Editor;

      // Get the IDs of the spaces we want to process
      // and simply call a function to process each

      ObjectId msId, psId;
      Transaction tr = db.TransactionManager.StartTransaction();
      
      using (tr)
      {
        BlockTable bt =
          (BlockTable)tr.GetObject(
            db.BlockTableId,
            OpenMode.ForRead
          );
        msId =
          bt[BlockTableRecord.ModelSpace];
        psId =
          bt[BlockTableRecord.PaperSpace];

        // Not needed, but quicker than aborting
        tr.Commit();
      }
      int msCount =
        UpdateAttributesInBlock(
          msId,
          blockName,
          attbName,
          attbValue         
              
        );
      int psCount =
        UpdateAttributesInBlock(
          psId,
          blockName,
          attbName,
          attbValue
        
        );
      ed.Regen();

      // Display the results

      ed.WriteMessage(
        "\nProcessing file: " + db.Filename
      );
      ed.WriteMessage(
        "\nUpdated {0} attribute{1} of CHTITLEBLOCK in the modelspace.",
        msCount,
        msCount == 1 ? "" : "s",
        attbName
      );
      ed.WriteMessage(
        "\nUpdated {0} attribute{1} of CHTITLEBLOCK in the default paperspace.",
        psCount,
        psCount == 1 ? "" : "s",
        attbName
      );
    }

    private int UpdateAttributesInBlock(
      ObjectId btrId,
      string blockName,
      string attbName,
      string attbValue
     
    )
    {
      // Will return the number of attributes modified

      int changedCount = 0;
      Document doc =
        Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
        DocumentLock doclock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument();
      Database db = doc.Database;
      Editor ed = doc.Editor;

      Transaction tr =
        doc.TransactionManager.StartTransaction();
      using (tr)
      {
        BlockTableRecord btr =
          (BlockTableRecord)tr.GetObject(btrId, OpenMode.ForRead);

        // Test each entity in the container...
        using (DocumentLock acLckDoc = doc.LockDocument())
        foreach (ObjectId entId in btr)
        {
          Entity ent =
            tr.GetObject(entId, OpenMode.ForRead)
            as Entity;

          if (ent != null)
          {
            BlockReference br = ent as BlockReference;
            if (br != null)
            {
              BlockTableRecord bd =
                (BlockTableRecord)tr.GetObject(
                  br.BlockTableRecord,
                  OpenMode.ForRead
              );

              // ... to see whether it's a block with
              // the name we're after

              if (bd.Name.ToUpper() == blockName)
              {

                // Check each of the attributes...

                foreach (
                  ObjectId arId in br.AttributeCollection
                )
                {
                  DBObject obj =
                    tr.GetObject(
                      arId,
                      OpenMode.ForRead
                    );
                  AttributeReference ar =
                    obj as AttributeReference;
                  if (ar != null)
                  {
                    // ... to see whether it has
                    // the tag we're after

                    if (ar.Tag.ToUpper() == attbName)
                    {
                      // If so, update the value
                      // and increment the counter

                      ar.UpgradeOpen();
                      ar.TextString = attbValue;
                      ar.DowngradeOpen();
                      changedCount++;
                    }
                                     
                   
                  }
                }
              }

              // Recurse for nested blocks
              changedCount +=
                UpdateAttributesInBlock(
                  br.BlockTableRecord,
                  blockName,
                  attbName,
                  attbValue
                 
                );
            }
          }
        }
        tr.Commit();
        tr.Dispose();
      }
      doclock.Dispose();
      return changedCount;
    }
  }

Vasant PADHIYAR
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.