Author Topic: Sheet & Key Note App  (Read 2683 times)

0 Members and 1 Guest are viewing this topic.

Jeff H

  • Needs a day job
  • Posts: 6150
Sheet & Key Note App
« on: November 02, 2010, 07:51:06 AM »
Link To ScreenCast

This is a starter for a Sheet and Key Note App
It is where you can type in sheet and key notes outside AutoCAD then use it to place the notes.

Yes I know it needs work but I thought I could get some ideas before I get serious with it, or you can do with it what you want.

If anyone is interested I am going set it up where you have projects and select the project and it will fill the sheet ListBox and you select from there to add or edit Sheet and keynotes.
It will go both ways updating from and to AutoCAD.
It will place the block for the keynote instead of number.
etc.....

It is HardCoded right now just to get it going, but if no one has any ideas are not interested I will leave it at this.
The drawing with Mleader, the VS solution for the netload, and seperate VS .exe are uploaded in the test folder.

Again this was slapped together in 30 minutes to get the idea across any suggestions would be appreciated

The netload App
Code: [Select]
using System.IO;
using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using System.Text;
[assembly: CommandClass(typeof(HpadCadSheetNotation.MyCommands))]
namespace HpadCadSheetNotation
{
    public class MyCommands
    {
        [CommandMethod("AddNotation")]
        public void AddNotation()
        {
            StringBuilder strBld = new StringBuilder();
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            Point3d mTxtStartPnt;           
            Point3d keyin;
            PromptDistanceOptions pdo = new PromptDistanceOptions("Enter ");
            pdo.UseBasePoint = true;
            pdo.UseDashedLine = true;
            pdo.BasePoint = ed.GetPoint("Enter").Value;
            PromptDoubleResult pdr = ed.GetDistance(pdo);
            if (pdr.Status != PromptStatus.OK)
            {
                return;
            }
            mTxtStartPnt = pdo.BasePoint;
            using (Transaction trx = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = (BlockTable)db.BlockTableId.GetObject(OpenMode.ForRead);
                BlockTableRecord btrMS = (BlockTableRecord)bt[BlockTableRecord.ModelSpace].GetObject(OpenMode.ForRead);
                BlockTableRecord mldrBlock = (BlockTableRecord)bt["KeyNote"].GetObject(OpenMode.ForRead);
                MText mTxt = new MText();
                mTxt.SetDatabaseDefaults();
                mTxt.Location = mTxtStartPnt;
                mTxt.Width = pdr.Value;
                StreamReader strReader = File.OpenText(@"C:\Test\TestTxt.txt");
               
                    string notes = null;                   
                    while (!((notes = strReader.ReadLine().ToUpper()) == "Key notes".ToUpper()))
                    {                       
                            strBld.AppendLine(notes.ToUpper());                                             
                    }                               
                    mTxt.Contents = strBld.ToString();
                    strBld.Remove(0, strBld.Length);
                btrMS.UpgradeOpen();
                btrMS.AppendEntity(mTxt);
                trx.AddNewlyCreatedDBObject(mTxt, true);
                db.TransactionManager.QueueForGraphicsFlush();
                Point3dCollection pntColl = new Point3dCollection();
                pntColl = mTxt.GetBoundingPoints();
                keyin = pntColl[2];// Get the bottom left corner of mtext to set new mtext
                MText mTxtKey = new MText();
                mTxtKey.Location = keyin;
                DBDictionary mLdrDict = db.MLeaderStyleDictionaryId.GetObject(OpenMode.ForRead) as DBDictionary;
                int keyNumber = 0;               
                strBld.AppendLine("\nKEY NOTES");
                while ((notes = strReader.ReadLine()) != null)
                {
                    strBld.AppendLine(notes.ToUpper());
                    if (notes !=  "")
                    {
                        MLeader mLdr = new MLeader();
                        mLdr.MLeaderStyle = mLdrDict.GetAt("KeyNoteLeader");
                        mLdr.BlockContentId = mldrBlock.Id;
                        PromptPointOptions ppo = new PromptPointOptions("\nSelect Point:");
                        Point3d tagPnt = ed.GetPoint(ppo).Value;
                        mLdr.BlockPosition = tagPnt;
                        ppo.UseBasePoint = true;
                        ppo.UseDashedLine = true;
                        ppo.BasePoint = tagPnt;
                        int leaderLine = mLdr.AddLeaderLine(ed.GetPoint(ppo).Value);                       
                        Matrix3d matx = Matrix3d.Displacement(mLdr.BlockPosition.GetAsVector());
                        foreach (ObjectId objId in mldrBlock)
                        {
                            AttributeDefinition attDef = objId.GetObject(OpenMode.ForRead) as AttributeDefinition;
                            if (attDef != null)
                            {
                                AttributeReference attRef = new AttributeReference();
                                attRef.SetAttributeFromBlock(attDef, matx);
                                attRef.Position = attDef.Position.TransformBy(matx);
                                attRef.TextString = (++keyNumber).ToString();
                                mLdr.SetBlockAttribute(objId, attRef);
                            }
                                                       
                        }
                        btrMS.AppendEntity(mLdr);
                        trx.AddNewlyCreatedDBObject(mLdr, true);
                        db.TransactionManager.QueueForGraphicsFlush();
                    }
                }
                mTxtKey.Contents = strBld.ToString();
                btrMS.AppendEntity(mTxtKey);
                trx.AddNewlyCreatedDBObject(mTxtKey, true);
                strReader.Close();
                trx.Commit();
             
            }
           
        }

       

    }

}


The separate .exe
Code: [Select]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Win32;
namespace HpadCadAnnotationApp
{
    public partial class frmMain : Form
    {

        public frmMain()
        {
            InitializeComponent();
        }

        private void btnUnderLine_Click(object sender, EventArgs e)
        {
            string tempText = "{L/" + shtNotetxtbox.SelectedText + "}";
            shtNotetxtbox.SelectedText = tempText;
        }

        private void Save_Click(object sender, EventArgs e)
        {
            saveFD.DefaultExt = ".txt";

            if (saveFD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                StringBuilder sb = new StringBuilder();
                StringReader srShtNts = new StringReader(shtNotetxtbox.Text);
                StringReader srKeyNts = new StringReader(keyNoteTxtbx.Text);
                StreamWriter sw = new StreamWriter(@"C:\Test\TestTxt.txt");
                string line;
                sb.AppendLine("Sheet Notes");
                while ((line = srShtNts.ReadLine()) != null)
                {
                    if (line.ToUpper() != "Sheet Notes".ToUpper())
                        sb.AppendLine(line.ToUpper());
                }
                srShtNts.Close();
                sb.AppendLine("\nKey Notes");
               
                while ((line = srKeyNts.ReadLine()) != null)
                {
                    if (line.ToUpper() != "Key Notes".ToUpper())
                        sb.AppendLine(line.ToUpper());
                }
                srKeyNts.Close();
                sw.Write(sb.ToString());
                sw.Close();
            }

        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            openFD.DefaultExt = ".txt";
            if (openFD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                StringBuilder sb = new StringBuilder();
                StreamReader strmReader = new StreamReader(openFD.FileName);
                 string notes = null;
                 while (!((notes = strmReader.ReadLine().ToUpper()) == "Key notes".ToUpper()))
                 {
                     if (notes.ToUpper() != "Sheet Notes".ToUpper())
                     sb.AppendLine(notes.ToUpper());
                 }
                 shtNotetxtbox.Text = sb.ToString();
                 sb.Remove(0, sb.Length);

                 while ((notes = strmReader.ReadLine()) != null)
                 {                     
                         sb.AppendLine(notes.ToUpper());
                 }
                 keyNoteTxtbx.Text = sb.ToString();
                 strmReader.Close();
            }
           
        }


     
    }
}