1
Layouts and printing / Re: Find out which paper size to use
« on: December 24, 2013, 02:47:55 PM »
layout.Extents;
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
[CommandMethod("drawline")]
static public void drawline()
{
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction tr = db.TransactionManager.StartTransaction())
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord ms = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
Line ln = new Line(new Point3d(10, 10, 0), new Point3d(20, 20, 0));
ms.AppendEntity(ln);
tr.AddNewlyCreatedDBObject(ln, true);
tr.Commit();
}
}
public class MyCommands
{
[CommandMethod("OrderLayout")]
public static void OrderAllLayouts()
{
Document dwg = Application.DocumentManager.MdiActiveDocument;
Editor ed = dwg.Editor;
SetLayoutOrder();
ed.Regen();
}
private static void SetLayoutOrder()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
var layouts = new Dictionary<string, Layout>();
using (Transaction tr = db.TransactionManager.StartTransaction())
{
DBDictionary layoutDICT = (DBDictionary)tr.GetObject(db.LayoutDictionaryId, OpenMode.ForRead);
ed.WriteMessage("\nBefore Sort");
long j = 0;
foreach (DBDictionaryEntry dictEntry in layoutDICT)
{
Layout layout = tr.GetObject(dictEntry.Value, OpenMode.ForRead) as Layout;
if (!layout.ModelType)
{
layouts.Add(layout.LayoutName, layout);
ed.WriteMessage("\n Layout[{0}]={1}, tab={2}", j++, layout.LayoutName, layout.TabOrder);
}
}
var sortedLayoutNames = layouts.Keys
.Where(n => n.Contains('-'))
.Select(n => n.Split(new char[] { '-' }))
.OrderBy(n => n[0])
.ThenBy(n => n[1])
.ThenBy(n => Convert.ToInt32(n[2]))
.Select(n => string.Join("-", n))
.Select((n, i) => { layouts[n].UpgradeOpen(); layouts[n].TabOrder = i + 1; return i + layouts[n].LayoutName; }).ToList();
//ed.WriteMessage("\nsortedLayoutNames");
//foreach (string sortedLayoutname in sortedLayoutNames)
// ed.WriteMessage("\n{0}", sortedLayoutname);
ed.WriteMessage("\nAfter Sort");
j = 0;
foreach (DBDictionaryEntry dictEntry in layoutDICT)
{
Layout layout = tr.GetObject(dictEntry.Value, OpenMode.ForRead) as Layout;
ed.WriteMessage("\n Layout[{0}]={1}, tab={2}", j++, layout.LayoutName, layout.TabOrder);
}
tr.Commit();
}
}
}
So it' possible to systematically remove the handler before adding it.But this may take a while if you need to guess how often you have to remove handlers.
if (!eventsEnabled)
Thanks Gile.
[CommandMethod("AddEv")]
public void AddEvents()
{
LayoutManager layMgr = LayoutManager.Current;
layMgr.LayoutSwitched += new Autodesk.AutoCAD.DatabaseServices.LayoutEventHandler(OnLayoutSwitched);
}
[CommandMethod("RemEv")]
public void RemoveEvents()
{
try
{
LayoutManager layMgr = LayoutManager.Current;
layMgr.LayoutSwitched -= new Autodesk.AutoCAD.DatabaseServices.LayoutEventHandler(OnLayoutSwitched);
}
catch
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
ed.WriteMessage("\nFailed to remove eventhandler");
}
}
public void OnLayoutSwitched(object senderObj, Autodesk.AutoCAD.DatabaseServices.LayoutEventArgs layoutEventArgs)
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
ed.WriteMessage("\nOnLayoutSwitched={0}", layoutEventArgs.Name);
}
public static class Class1
{
private bool eventsEnabled;
public void EnableEvents()
{
if (!eventsEnabled)
{
LayoutManager.Current.LayoutSwitched += new Autodesk.AutoCAD.DatabaseServices.LayoutEventHandler(OnLayoutSwitched);
eventsEnabled = true;
...
public static class KaeferExtensions
{
// Opens a DBObject in ForRead mode (kaefer @ TheSwamp)
public static T GetObject<T>(this ObjectId id) where T : DBObject
{
return id.GetObject<T>(OpenMode.ForRead);
}
// Opens a DBObject in the given mode (kaefer @ TheSwamp)
public static T GetObject<T>(this ObjectId id, OpenMode mode) where T : DBObject
{
return id.GetObject(mode) as T;
}
// Opens a collection of DBObject in ForRead mode (kaefer @ TheSwamp)
public static IEnumerable<T> GetObjects<T>(this IEnumerable ids) where T : DBObject
{
return ids.GetObjects<T>(OpenMode.ForRead);
}
// Opens a collection of DBObject in the given mode (kaefer @ TheSwamp)
public static IEnumerable<T> GetObjects<T>(this IEnumerable ids, OpenMode mode) where T : DBObject
{
return ids
.Cast<ObjectId>()
.Select(id => id.GetObject<T>(mode))
.Where(res => res != null);
}
// Applies the given Action to each element of the collection (mimics the F# Seq.iter function).
public static void Iterate<T>(this IEnumerable<T> collection, Action<T> action)
{
foreach (T item in collection) action(item);
}
// Applies the given Action to each element of the collection (mimics the F# Seq.iteri function).
// The integer passed to the Action indicates the index of element.
public static void Iterate<T>(this IEnumerable<T> collection, Action<T, int> action)
{
int i = 0;
foreach (T item in collection) action(item, i++);
}
}
...
//using KeaderExtensions:
pvpFrozenLayerNames = lt.GetObjects<LayerTableRecord>()
.Where(ly => ly.IsOff)
.Where(ly => ly.IsFrozen)
.Select(ly => ly.Name)
.ToList();
using (Transaction tr = db.TransactionManager.StartTransaction())
{
LayerTable lt = (LayerTable)db.LayerTableId.GetObject(OpenMode.ForRead);
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord ms = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
BlockTableRecord ps = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.PaperSpace], OpenMode.ForWrite);
LayoutManager layMgr = LayoutManager.Current;
Layout lay = (Layout)tr.GetObject(layMgr.GetLayoutId(layMgr.CurrentLayout), OpenMode.ForRead);
//Collect LayerNames to exclude e.q. Layers OFF or Frozen
List<string> frozenLayerNames= lt
.Cast<ObjectId>()
.Select(id => (LayerTableRecord)tr.GetObject(id, OpenMode.ForRead))
.Where(ly => ly.IsOff)
.Where(ly => ly.IsFrozen)
.Select(ly => ly.Name)
.ToList();
//Same but now from within PaperSpace Viewports
List<string> pvpFrozenLayerNames = lay
.GetViewports().Cast<ObjectId>()
.Select(id => (Viewport)tr.GetObject(id, OpenMode.ForRead))
.SelectMany(pvp => pvp.GetFrozenLayers().Cast<ObjectId>())
.Distinct().ToArray()
.Select(id => (LayerTableRecord)tr.GetObject(id, OpenMode.ForRead))
.Select(ly => ly.Name)
.ToList();
// List (visible) Layers named "...~..." and "...TXT..." and NOT "...NOTTOBEINCLUDED..."
var visLayerNames = lt
.Cast<ObjectId>()
.Select(id => (LayerTableRecord)tr.GetObject(id, OpenMode.ForRead))
.Where(ly => ly.Name.Contains('~'))
.Where(ly => ly.Name.IndexOf("TXT", StringComparison.CurrentCultureIgnoreCase) > 0)
.Where(ly => ly.Name.IndexOf("NOTTOBEINCLUDED", StringComparison.CurrentCultureIgnoreCase) < 0)
.Where(ly => !ly.IsOff)
.Where(ly => !ly.IsFrozen)
.Where(ly => !pvpFrozenLayerNames.Contains(ly.Name))
.Select(ly => ly.Name)
.ToList();
//select ModelSpace Text from specific layers
var visTxt = ms.Cast<ObjectId>()
.Where(id => id.ObjectClass.DxfName.ToUpper() == "TEXT" || id.ObjectClass.DxfName.ToUpper() == "MTEXT")
.Select(id => (Entity)tr.GetObject(id, OpenMode.ForRead))
.Where(ent => visLayerNames.Contains(ent.Layer))
.ToList();
//select Text from specific layers within specific Blocks
var visTxtInSpecificBlks = ms.Cast<ObjectId>()
.Where(id => id.ObjectClass.DxfName.ToUpper() == "INSERT")
.Select(id => (BlockReference)tr.GetObject(id, OpenMode.ForRead))
.Where(blkRef => visLayerNames.Contains(blkRef.Layer))
.Where(blkRef => blkRef.Name.IndexOf("-xxx-", StringComparison.CurrentCultureIgnoreCase) > 0)
.SelectMany(blkRef => ((BlockTableRecord)tr.GetObject(bt[blkRef.Name], OpenMode.ForRead)).Cast<ObjectId>())
.Where(id => id.ObjectClass.DxfName.ToUpper() == "TEXT" || id.ObjectClass.DxfName.ToUpper() == "MTEXT")
.Select(id => (Entity)tr.GetObject(id, OpenMode.ForRead))
.Where(ent => visLayerNames.Contains(ent.Layer))
.ToList();
//Join Modelspace and Block Specific text
visTxt.AddRange(visTxtInSpecificBlks);
// build regular pattern to select certain string contents
// use regex tester to help build the search pattern.
string searchNrs = @"(^3\(|^)(...)(.*)";
string returnNrs = "$2";
var matchNr = new Regex(searchNrs , RegexOptions.IgnoreCase | RegexOptions.Compiled);
//find visible text containing a complex patter
var textNrs = visTxt
.Select(ent => GetType().Name == "MTEXT" ? ((MText)ent).Text : ((DBText)ent).TextString)
.Select(txt => matchNr.Replace(txt, searchNrs))
.Distinct().ToArray()
.ToList();
//List in numeric order
kabelNrs = kabelNrs
.OrderBy(kabelNr => kabelNr.Length)
.ThenBy(kabelNr => kabelNr)
.ToList();
...