Hey Everyone,
I've been working on this code for a bit now and have recently gotten some excellent assitance from a few members here. I'm stuck on something small here and wondering if anyone could lend a hand. I've got a Windows Form with a numericupdownbox which is designed to determine how many Layouts (PaperSpace) that a user would like generated. I've got the form to pass the value as to the function as an argument but an not sure how to get the function to automatically generate "x" amount of Layouts based on the value of "int x". Right now i've just got a few "if" statements controlling the system, but i'm looking for a more elegant solution than a sea of "if" statements. Any suggestions would be excellent.
namespace CHPaperSpace
{
public class PaperSpace
{
[Autodesk.AutoCAD.Runtime.CommandMethod("CHPAPERSPACE")]
// This will be the function associated with my command
public void CHPAPERSPACECOMMAND(int x)
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
Autodesk.AutoCAD.DatabaseServices.TransactionManager trm = db.TransactionManager;
using (DocumentLock acLckDoc = doc.LockDocument())
try
{
Transaction tr = trm.StartTransaction();
LayoutManager oLayoutManager = Autodesk.AutoCAD.DatabaseServices.LayoutManager.Current;
try
{
ObjectId oLayoutObjectId;
if (x == 2)
{
oLayoutObjectId = oLayoutManager.CreateLayout("This is 2");
Layout oLayout = (Layout)tr.GetObject(oLayoutObjectId, OpenMode.ForWrite);
oLayout.Initialize();
}
if (x == 3)
{
oLayoutObjectId = oLayoutManager.CreateLayout("This is 2");
oLayoutObjectId = oLayoutManager.CreateLayout("This is 3");
Layout oLayout = (Layout)tr.GetObject(oLayoutObjectId, OpenMode.ForWrite);
oLayout.Initialize();
}
ed.Regen();
}
finally
{
tr.Commit();
tr.Dispose();
}
}
catch
{ }
}
}
}
Solved! Go to Solution.
Solved by jeff. Go to Solution.
{ foreach (string item in myCollection) { oLayoutObjectId = oLayoutManager.CreateLayout(item); } }
Thanks for the assistance, although i'm not exactly sure how to implement this. How does this use the value of the variable to create each "item"? Also, when I try to implement this, i get the error that the "foreach staatement cannot operate on variables of type "int" because "int" does not contain a public definition for "Get Enumerator". Please forgive my ignorance, i'm pretty new at this and trying to get a good handle.
Thanks
Vince
It might be best to create a ADT to use instead of passing around a int or list, that way you handle the complexity of different settings, but its interface easy to deal with.
Basicly you are just doing a loop
Quickly thrown together sample of a basic for loop
[CommandMethod("CreateLayouts")] public void CreateLayouts() { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; int num = ed.GetInteger("\nEnter Number of Layouts").Value; CreateLayouts(num); } private void CreateLayouts(int numLayouts) { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; using (Transaction trx = db.TransactionManager.StartTransaction())// Start Transaction { LayoutManager lm = LayoutManager.Current; for (int i = 1; i <= numLayouts; i++) { Layout layout = (Layout)trx.GetObject(lm.CreateLayout("This is" + i.ToString()), OpenMode.ForWrite); layout.Initialize(); } trx.Commit(); } }
It needs to implement IEnumerable, but if you think about a int or a number how would itterate over a number besides maybe read the bytes, but it would not make much sense for a int to implement IEnumerable
The previous example would be a collection of strings---"L1", "L2", "L3",.......
Your .NET collections already implement IEnumerable which requires it to have a GetEnumerator method.
That actually makes sense. I've modified that code a bit and it works perfectly. Thank you very much jeff and also many thanks to arcticad for the assistance! This community is incredible and I appreciate all the help.