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
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