Author Topic: Xref Clean-up  (Read 3101 times)

0 Members and 1 Guest are viewing this topic.

Jeff H

  • Needs a day job
  • Posts: 6144
Xref Clean-up
« on: September 24, 2010, 10:29:13 AM »
This is first AutoCad Application I ever wrote about 6 months to a year ago. I thought I would load it up for a laugh or if someone wanted to clean it up. I use to only code in VB but I switched to C# and
C# use to irritate me now VB irritates me so I just typed it out to C# and not much updating I left some things out but it is pretty much it.

It pretty much let's you chose a color and changes all layers, or and entities to that color.
It will freeze all hatches but places them on the layer name + - Frozen . So it will just freeze the hatch and not anything else on the layer.
It will remove wipeouts and if it dynamic will explode wipeout and all the other visibility states will not show when exploded.
It will remove mask text background

This is drawing I used to test it on that is so full of stuff it was a good to use, but since it's classified I can not zoom out much
Here is the basic main part of the code
And Added the project
Code: [Select]

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Autodesk.AutoCAD;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Windows;
namespace HpadCadXrefCleanUp
{
    public partial class HpadCadXrefCleanUpPalette : UserControl
    {
   
        public HpadCadXrefCleanUpPalette()
        {
            InitializeComponent();
        }
       
       

        private void btnColor_Click(object sender, EventArgs e)
        {
            try
            {
                Autodesk.AutoCAD.Windows.ColorDialog cd = new Autodesk.AutoCAD.Windows.ColorDialog();
                cd.IncludeByBlockByLayer = true;
                if (cd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    Autodesk.AutoCAD.Colors.Color clr = cd.Color;
                    short clrIndx = clr.ColorIndex;
                    this.txtColorIndex.Text = clrIndx.ToString();
                    this.lblColor.BackColor = CadColors.AcadColorIndexToRGB(clrIndx);
                }
            }
            catch
            {
                Autodesk.AutoCAD.Runtime.Exception ex = new Autodesk.AutoCAD.Runtime.Exception();
                System.Windows.Forms.MessageBox.Show(ex.Message.ToString());

            }

        }

        private void BtnUpdateXref_Click(object sender, EventArgs e)
        {
            short cltIdx = Convert.ToInt16(this.txtColorIndex.Text);
            if (this.chkbxRemoveWipeouts.Checked == true)
            {
                HpadCadXrefCleanUpHelper.RemoveWipeouts();
            }
            if (this.chkbxFreezeHatches.Checked == true)
            {
                HpadCadXrefCleanUpHelper.FreezeHatches(cltIdx);
            }
            if (this.chkbxRemoveTextBackground.Checked == true)
            {
                HpadCadXrefCleanUpHelper.RemoveTextBackgroundMask();
            }
            if (this.chkbxChangeLayerColor.Checked == true)
            {
                HpadCadXrefCleanUpHelper.ChangeAllLayerColors(cltIdx);
            }
            if (this.chkbxChangeObjectColor.Checked == true)
            {
                HpadCadXrefCleanUpHelper.ChangeAllEntityColors(cltIdx);
            }


        }

     
    }  // End partial class HpadCadXrefCleanUpPalette
    public  class HpadCadXrefCleanUpHelper
    {
        private static Autodesk.AutoCAD.Windows.PaletteSet xrefPS;
        public  void XrefPaletteIntialize()
        {
            Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
            try
            {

                if (xrefPS == null)
                {
                    xrefPS = new Autodesk.AutoCAD.Windows.PaletteSet("Hpad XrefCleanUp", new Guid("5DABE040-A3C7-4E40-B951-C60B2C209E52"));
                    xrefPS.Style = PaletteSetStyles.ShowTabForSingle;
                    xrefPS.Style = PaletteSetStyles.NameEditable;
                    xrefPS.Style = PaletteSetStyles.ShowAutoHideButton;
                    xrefPS.Style = PaletteSetStyles.ShowPropertiesMenu;
                    xrefPS.Style = PaletteSetStyles.ShowCloseButton;
                    xrefPS.Style = PaletteSetStyles.SingleRowNoVertResize;
                    xrefPS.Style = PaletteSetStyles.SingleColDock;
                    xrefPS.Style = PaletteSetStyles.SingleRowDock;

                    xrefPS.Opacity = 90;

                    xrefPS.MinimumSize = new System.Drawing.Size(50, 50);
                    System.Windows.Forms.UserControl xrefpt = new HpadCadXrefCleanUpPalette();
                    xrefPS.Add("Xref", xrefpt);
                    xrefPS.Visible = true;
                    xrefPS.DockEnabled = DockSides.Right;
                    xrefPS.DockEnabled = DockSides.Left;
                    xrefPS.AutoRollUp = true;
                }
                else
                {
                    if (xrefPS.Visible == false)
                    {
                        xrefPS.Visible = true;

                    }
                }

            }
            catch (System.Exception ex)
            {
                ed.WriteMessage("Error in Palette command: " + ex.Message);
            }
        }
        public static void RemoveWipeouts()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            using (DocumentLock docLock = doc.LockDocument())
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                    foreach (ObjectId objId in bt)
                    {
                        BlockTableRecord btr = objId.GetObject(OpenMode.ForRead) as BlockTableRecord;
                        foreach (ObjectId btrObjId in btr)
                        {
                            Entity ent = btrObjId.GetObject(OpenMode.ForRead) as Entity;
                            if (ent is Wipeout)
                            {
                                ent.UpgradeOpen();
                                if (ent.Visible == true)
                                {
                                    DBObjectCollection objColl = new DBObjectCollection();
                                    ent.Explode(objColl);
                                    foreach (Entity exEnt in objColl)
                                    {
                                        btr.UpgradeOpen();
                                        btr.AppendEntity(exEnt);
                                        tr.AddNewlyCreatedDBObject(exEnt, true);
                                    }
                                }
                            }
                        }
                    }
                    tr.Commit();
                }// End TransAction
            }// End DocumentLock
        }// End RemoveWipeOuts
        public static void RemoveTextBackgroundMask()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            using (DocumentLock docLock = doc.LockDocument())
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                    foreach (ObjectId objId in bt)
                    {
                        BlockTableRecord btr = objId.GetObject(OpenMode.ForRead) as BlockTableRecord;
                        foreach (ObjectId btrObjId in btr)
                        {
                            Entity ent = btrObjId.GetObject(OpenMode.ForRead) as Entity;
                            if (ent is MText)
                            {                               
                                MText mt = ent.Id.GetObject(OpenMode.ForWrite) as MText;
                                mt.BackgroundFill = false;
                            }
                        }
                    }
                    tr.Commit();
                }// End TransAction
            }// End DocumentLock
        }// End RemoveTextBackgroundMask
        public static void ChangeAllLayerColors(short clrIdx)
        {
            Database db = HostApplicationServices.WorkingDatabase;
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            using (DocumentLock docLock = doc.LockDocument())
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                    foreach (ObjectId objId in lt)
                    {
                        LayerTableRecord ltr = objId.GetObject(OpenMode.ForRead) as LayerTableRecord;
                        if (ltr.Color != Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByAci,clrIdx))
                        {
                            ltr.UpgradeOpen();
                            ltr.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByAci,clrIdx);
                        }
                       
                    }
                    tr.Commit();
                }// End TransAction
            }// End DocumentLock
        }// End ChangeAllLayerColors
        public static void ChangeAllEntityColors(short clrIdx)
        {
            Database db = HostApplicationServices.WorkingDatabase;
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            using (DocumentLock docLock = doc.LockDocument())
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                    foreach (ObjectId objId in bt)
                    {
                        BlockTableRecord btr = objId.GetObject(OpenMode.ForRead) as BlockTableRecord;
                        foreach (ObjectId btrObjId in btr)
                        {
                            Entity ent = btrObjId.GetObject(OpenMode.ForWrite) as Entity;
                            ent.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByAci, clrIdx);
                            if (ent is BlockReference)
                            {
                                BlockReference bref = ent.Id.GetObject(OpenMode.ForRead) as BlockReference;
                                Autodesk.AutoCAD.DatabaseServices.AttributeCollection attColl = bref.AttributeCollection;
                                foreach (ObjectId attId in attColl)
                                {
                                    AttributeReference attRef = attId.GetObject(OpenMode.ForWrite) as AttributeReference;
                                    attRef.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByAci, clrIdx);
                                }

                            }
                        }
                    }
                    tr.Commit();
                }// End TransAction
            }// End DocumentLock
        }// End ChangeAllEntityColors
        public static void FreezeHatches(short clrIdx)
        {
            Database db = HostApplicationServices.WorkingDatabase;
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            using (DocumentLock docLock = doc.LockDocument())
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                    LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                    foreach (ObjectId objId in bt)
                    {
                        BlockTableRecord btr = objId.GetObject(OpenMode.ForRead) as BlockTableRecord;
                        foreach (ObjectId btrObjId in btr)
                        {
                            Entity ent = btrObjId.GetObject(OpenMode.ForRead) as Entity;                           
                            if (ent is Hatch)
                            {
                                string newLayName = ent.Layer + "-Frozen";
                                ent.UpgradeOpen();
                                if (!(lt.Has(newLayName)))
                                {
                                    LayerTableRecord ltr = new LayerTableRecord();
                                    ltr.Name = newLayName;
                                    ltr.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByAci, clrIdx);                                   
                                    lt.UpgradeOpen();
                                    lt.Add(ltr);
                                    tr.AddNewlyCreatedDBObject(ltr, true);
                                    ent.Layer = newLayName;
                                    ltr.IsFrozen = true;
                                }
                                ent.Layer = newLayName;
                            }
                        }
                    }
                    tr.Commit();
                }// End TransAction
            }// End DocumentLock
        }// End FreezeHatches


    }// End class HpadCadXrefCleanUpHelper
}// End NameSpace

 
« Last Edit: September 24, 2010, 10:33:05 AM by fro2001 »

Glenn R

  • Guest
Re: Xref Clean-up
« Reply #1 on: September 24, 2010, 11:19:18 AM »
A little tip with nested 'using' statements. Instead of doing this:

Code: [Select]
using (DocumentLock docLock = doc.LockDocument())
{
    using (Transaction tr = db.TransactionManager.StartTransaction())
    {
       // mojo goes here
    }
}

you can 'stack' them, like so:

Code: [Select]
using (DocumentLock docLock = doc.LockDocument())
using (Transaction tr = db.TransactionManager.StartTransaction())
{
  // mojo goes here
}

It's just a little bit clearer, but not used that often.

Jeff H

  • Needs a day job
  • Posts: 6144
Re: Xref Clean-up
« Reply #2 on: September 24, 2010, 11:32:47 AM »
Thanks Glenn
And there is a file in there CadColors witch is about 500 or so lines
You can get the idea from the snippet below. I think I got it from Kean's blog
I am right by not putting a break; in it when the it has a return statement.
Code: [Select]
public static System.Drawing.Color AcadColorIndexToRGB(short aci)
        {
            switch (aci)
            {
                case 1:
                    return Color.FromArgb(255, 0, 0);                   
                case 2:
                    return Color.FromArgb(255, 255, 0);                   
                case 3:
                    return Color.FromArgb(0, 255, 0);
                case 4:
                    return Color.FromArgb(0, 255, 255);



Glenn R

  • Guest
Re: Xref Clean-up
« Reply #3 on: September 24, 2010, 03:59:23 PM »
I am right by not putting a break; in it when the it has a return statement.

That's a statement, but I assume you're asking a question? If so, then yes, that's correct - you don't need a break if you're using 'return'.

Jeff H

  • Needs a day job
  • Posts: 6144
Re: Xref Clean-up
« Reply #4 on: September 24, 2010, 04:13:56 PM »
Thanks again?