Author Topic: Layout creation tool  (Read 16859 times)

0 Members and 2 Guests are viewing this topic.

vegbruiser

  • Guest
Layout creation tool
« on: July 23, 2008, 04:22:40 AM »
In VBA I have created a routine that allows the user to create a series of layouts based on the position, rotation, scale and user-input-values (attributes for the resultant viewport scale etc.) of a particular "viewport" block in Modelspace and I was wondering about the best way to convert it to .NET.

Can anyone give me some pointers on where I should start?

(or should I just go for it and post the results here?)

sinc

  • Guest
Re: Layout creation tool
« Reply #1 on: July 23, 2008, 10:46:43 AM »
Go for it, and if you have specific questions about how to perform certain tasks in .NET, ask those.

.NET is a pretty big topic, so it's a lot easier to answer specific questions.

vegbruiser

  • Guest
Re: Layout creation tool
« Reply #2 on: July 31, 2008, 01:34:03 PM »
Well, I finally got stuck into making this thing. Here's what I've got so far: -
(this is my first attempt at a program in C# - hence the cobbled-together nature of what I've done here - also, thanks to the guys whose code I've borrowed. - I did this whilst at work today, so the link I got the original .zip file from I'll have to dig out tomorrow. )

One thing that bugs me about this code is that when I tried it earlier (with my boss watching) it didn't work the first time. Yet, when I ran it again, it did exactly what it should. Any ideas?

Also I've uploaded the viewport-block.dwg file so you C# gurus can properly test it (assuming you might want to)

Quote
Damnit, I'll upload the entire solution - the code is too many characters. :|

vegbruiser

  • Guest
Re: Layout creation tool
« Reply #3 on: August 06, 2008, 12:33:55 PM »
Here's what I've got today: -

It inserts the block no problem, and, up until I borrowed the attribute support code (from Kean) it was inserting the second block no problem too. Could someone take a look and tell me why it's not working?

I've debugged it right the way through and it gets as far as the following function.

Code: [Select]
public void InsertBlock(ObjectId Blockid, int ISPOSP) // Point3d BlockPos, Double BlockScale, RotationAngle BlockAngle,int ISPOSP)
        {
            //this assumes that because the viewport block has been copied to the drawing, the other necessary blocks will have been too.
            // Blockid = the id of the block we inserted using the jig.
            // BlockPos = the position of the above block.
            // Blockscale = the scale of the above block.
            // BlockAngle = the rotation angle of the above block.
            Database db = HostApplicationServices.WorkingDatabase;
            Document dwg = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Editor ed = dwg.Editor;
            BlockTable bt = null;
            BlockTableRecord btr = null;
            Point3d BlockPos = new Point3d(0, 0, 0);
            Scale3d BlockScale = new Scale3d(1);
            Double BlockAngle = 0;
            BlockReference blkref = null;
            BlockReference frameref = null;
            PromptStringOptions prso = null;
            PromptKeywordOptions prko = null;
            PromptResult res = null;
 
            // this next section adds the ISPviewport-frame block on top of the already-inserted ISPViewport block.
#region InsertISPViewportframe
            if (ISPOSP == 0) // Viewport = ISP
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead, false) as BlockTable;
                    foreach (ObjectId btrid in bt)
                    {
                        if (btrid == Blockid)
                        // we found the correct block?
                        {
                            btr = tr.GetObject(btrid, OpenMode.ForRead, false) as BlockTableRecord;
                            // normally we could filter out layouts, xrefs etc. but since we already know the objectid of the block
                            // we're looking for we don't need to bother.
                            if (btr.IsLayout || btr.IsFromExternalReference || !btr.IsDynamicBlock || btr.IsFromOverlayReference || !btr.HasAttributeDefinitions)
                            {
                                continue;
                            }
                            ObjectIdCollection blkrefIds = btr.GetBlockReferenceIds(true, false);
                            if (blkrefIds == null || blkrefIds.Count == 0) // this is where the code now stops. If you comment out the attribute support code, it works no problem??
                                break;
                            //int counter = 0;
                            foreach (ObjectId blkrefid in blkrefIds)
                            {
                                blkref = tr.GetObject(blkrefid, OpenMode.ForRead, false) as BlockReference;
                                BlockPos = blkref.Position;
                                BlockScale = blkref.ScaleFactors;
                                BlockAngle = blkref.Rotation;
                               
                                blkref = tr.GetObject(blkrefid, OpenMode.ForRead, false) as BlockReference;
                                AttributeCollection attrefids = blkref.AttributeCollection;
                                foreach (ObjectId attrefid in attrefids)
                                {
                                    AttributeReference attref = tr.GetObject(attrefid, OpenMode.ForWrite, false) as AttributeReference;
                                    switch (attref.Tag)
                                    {
                                        case "VIEWNO":
                                            prso = new PromptStringOptions("\nWhat view number is this?");
                                            res = ed.GetString(prso);
                                            if (res.Status == PromptStatus.OK)
                                            {
                                                attref.UpgradeOpen();
                                                string update = res.StringResult;
                                                attref.TextString = update;
                                                attref.DowngradeOpen();
                                            }
                                            break;
                                        case "FULLORPARTIAL":
                                            prko = new PromptKeywordOptions("\nWhat floor does this view depict?");
                                            prko.Keywords.Add("FULL");
                                            prko.Keywords.Add("PARTIAL");
                                            prko.Keywords.Add("DC");
                                            prko.Keywords.Add("ELEVATION");
                                            res = ed.GetKeywords(prko);
                                            if (res.Status == PromptStatus.OK)
                                            {
                                                attref.UpgradeOpen();
                                                string update = res.StringResult;
                                                attref.TextString = update;
                                                attref.DowngradeOpen();
                                            }
                                            break;
                                        case "FLOOR":
                                            prko = new PromptKeywordOptions("\nWhat floor does this view depict?");
                                            prko.Keywords.Add("GROUND");
                                            prko.Keywords.Add("FIRST");
                                            prko.Keywords.Add("SECOND");
                                            prko.Keywords.Add("THIRD");
                                            prko.Keywords.Add("FOURTH");
                                            prko.Keywords.Add("OTHER");
                                            PromptResult getWhichFloorResult = ed.GetKeywords(prko);
                                            if (getWhichFloorResult.StringResult == "OTHER")
                                            {
                                                res = ed.GetString("\nEnter another floor that's not listed.");
                                            }
                                            else
                                            {
                                                res = ed.GetKeywords(prko);
                                            }
                                            if (res.Status == PromptStatus.OK)
                                            {
                                                attref.UpgradeOpen();
                                                string update = res.StringResult;
                                                attref.TextString = update;
                                                attref.DowngradeOpen();
                                            }
                                            break;
                                    }
                                }
                                btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                                frameref = new BlockReference(BlockPos, bt["ISPVIEWPORT-FRAME"]);
                                frameref.ScaleFactors = BlockScale;
                                frameref.Rotation = BlockAngle;
                                btr.AppendEntity(frameref);
                                tr.AddNewlyCreatedDBObject(frameref, true);
                            }
                        }
                    }
                    tr.Commit();
                }
#endregion //InsertISPViewportframe
            }
        }
In case people are wondering, the attribute support code looks like this: -

Code: [Select]
        public void StartInsert(String JigBlockName,int ISPOSP)
        {
            Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
            Database db = HostApplicationServices.WorkingDatabase; // Application.DocumentManager.MdiActiveDocument.Database;
            try
            {
                Vector3d Normal = db.Ucsxdir.CrossProduct(db.Ucsydir);
                PromptResult res; // = ed.GetString(opts);
                if (JigBlockName != string.Empty)
                {
                    ObjectId block = GetBlockId(db, JigBlockName);
                    block = GetBlockId(db, JigBlockName);
                    if (block.IsNull)
                    {
                        ed.WriteMessage("\nBlock {0} not found.", JigBlockName);
                        return;
                    }
 
                    InsertJig jig = new InsertJig(block, Point3d.Origin, Normal.GetNormal());
 
                    //blockRotation 
                    if (vRotationUse == true)
                    {
                        jig.RotationAngleUse = true;
                        jig.RotationAngleValue = vRotationVal;
                    }
                    else
                    {
                        jig.RotationAngleUse = false;
                    }
               
                    jig.ScaleValue = vScaleVal;
                    jig.JigPromptCounter = 0; // Start with insertpoint
 
                    using (DocumentLock docLock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument())
                    {
                        res = ed.Drag(jig); // First Jig option..[Insertpoint]
 
                        // Should it accept a rightclick as the 0-point of the drawing?
                        // or does that cancel the operation...?
                        if (res.Status == PromptStatus.OK)
                        {
                            jig.UpdateBlockRef(); // Check if it returns True!!!
                            // Here also the Rotation-Jig should Start...
                            // if Rotation was fixed this should have been set at first...
                            // Should Accept rightclick as 0-rotation
                            if (vRotationUse == true)
                            {
                                // Ready...end the Jig
                            }
                            else
                            {
                                // Start a new Jig for the rotation..
                                jig.JigPromptCounter = 1;
                                res = ed.Drag(jig);
                                if (res.Status == PromptStatus.OK)
                                {
                                    jig.UpdateBlockRef();
                                    //Set a bite to check if it should continue...(esc => no block placed!!)
                                }
                            }
 
                            // if jig.RotationAngleUse = false then do it...
                            // jig.RotationAngleValue = 1; // Or skip this altogether if it's a given rotation...
                            // No checking to be done by Jig(ger)..??
 
                            Autodesk.AutoCAD.DatabaseServices.TransactionManager tm = db.TransactionManager;
 
 
                            if (res.Status == PromptStatus.OK) // Second check needed if there was a rotation Jig..
                            {
                                using (Transaction tr = tm.StartTransaction())
                                {
 
                                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite, false);
                                    btr.AppendEntity(jig.BlockReference);
                                    tr.AddNewlyCreatedDBObject(jig.BlockReference, true);
                                    // Start attribute support code
                                    BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId,OpenMode.ForRead);
 
                                    ObjectId bdId = bt[JigBlockName];
                                    btr = (BlockTableRecord)tr.GetObject(bdId,OpenMode.ForRead);
                                    // Add the attributes
                                    foreach (ObjectId attId in btr)
                                    {
                                        Entity ent =(Entity)tr.GetObject(attId,OpenMode.ForRead);
                                        if (ent is AttributeDefinition)
                                        {
                                            AttributeDefinition ad =(AttributeDefinition)ent;
                                            AttributeReference ar =new AttributeReference();
                                            ar.SetAttributeFromBlock(ad, jig.BlockReference.BlockTransform);
                                            jig.BlockReference.AttributeCollection.AppendAttribute(ar);
                                            tr.AddNewlyCreatedDBObject(ar, true);
                                        }
                                    }
                                    // End attribute support code
 
                                    // Call a function to make the graphics display
                                    // (otherwise it will only do so when we Commit)
                                    tm.QueueForGraphicsFlush();
                                    tr.Commit();
                                }
                            }
                        }
                    }
                    // Insert the viewportframe block (if ISP) here and update the attributes in the Viewport block.
                    InsertBlock(block, ISPOSP);
                }
               
 
            }
            catch (Autodesk.AutoCAD.Runtime.Exception ex)
            {
                ed.WriteMessage("\nThe Error was: " + ex.Message);
                //return false;
            }
            db.Dispose();
        }

vegbruiser

  • Guest
Re: Layout creation tool
« Reply #4 on: August 11, 2008, 07:23:00 AM »
Right, after not looking at this for a couple of days, I have fixed the attribute not inserting issue.

My insertblock command now looks like this: -

Code: [Select]
public void InsertBlock(ObjectId Blockid, int ISPOSP)
        {
            //this assumes that because the viewport block has been copied to the drawing, the other necessary blocks will have been too.
            // Blockid = the id of the block we inserted using the jig.
            // BlockPos = the position of the above block.
            // Blockscale = the scale of the above block.
            // BlockAngle = the rotation angle of the above block.
            Database db = HostApplicationServices.WorkingDatabase;
            Document dwg = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Editor ed = dwg.Editor;
            BlockTable bt = null;
            BlockTableRecord btr = null;
            Point3d BlockPos = new Point3d(0, 0, 0);
            Scale3d BlockScale = new Scale3d(1);
            Double BlockAngle = 0;
            BlockReference blkref = null;
            BlockReference frameref = null;
            PromptStringOptions prso = null;
            PromptKeywordOptions prko = null;
            PromptResult res = null;
 
            // this next section adds the ISPviewport-frame block on top of the already-inserted ISPViewport block.
#region InsertISPViewportframe
            if (ISPOSP == 0) // Viewport = ISP
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead, false) as BlockTable;
                    foreach (ObjectId btrid in bt)
                    {
                        if (btrid == Blockid)
                        // we found the correct block?
                        {
                            btr = tr.GetObject(btrid, OpenMode.ForRead, false) as BlockTableRecord;
                            // normally we could filter out layouts, xrefs etc. but since we already know the objectid of the block
                            // we're looking for we don't need to bother.
                            if (btr.IsLayout || btr.IsFromExternalReference || !btr.IsDynamicBlock || btr.IsFromOverlayReference || !btr.HasAttributeDefinitions)
                            {
                                continue;
                            }
                            ObjectIdCollection blkrefIds = btr.GetBlockReferenceIds(true, false);
                            if (blkrefIds == null || blkrefIds.Count == 0)
                                break;
                            //int counter = 0;
                            foreach (ObjectId blkrefid in blkrefIds)
                            {
                                blkref = tr.GetObject(blkrefid, OpenMode.ForWrite, false) as BlockReference;
                                BlockPos = blkref.Position;
                                BlockScale = blkref.ScaleFactors;
                                BlockAngle = blkref.Rotation;
                                //Start newly placed attribute start code.
                                BlockTable newbt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                                ObjectId bdid = bt[blkref.Name];
                                BlockTableRecord newbtr = (BlockTableRecord)tr.GetObject(bdid, OpenMode.ForRead);
                                //Add the attributes to the block definition otherwise we won't be able to edit them in blkref?
                                if (newbtr.HasAttributeDefinitions)
                                {
                                    foreach (ObjectId attid in newbtr)
                                    {
                                        AttributeDefinition attdef = tr.GetObject(attid, OpenMode.ForRead) as AttributeDefinition;
                                        if (attdef != null)
                                        {
                                            AttributeReference ar = new AttributeReference();
                                            ar.SetAttributeFromBlock(attdef, blkref.BlockTransform);
                                            blkref.AttributeCollection.AppendAttribute(ar);
                                            tr.AddNewlyCreatedDBObject(ar, true);
                                        }
                                    }
                                }
                                blkref = tr.GetObject(blkrefid, OpenMode.ForRead, false) as BlockReference;
                                AttributeCollection attrefids = blkref.AttributeCollection;
                                foreach (ObjectId attrefid in attrefids)
                                {
                                    AttributeReference attref = tr.GetObject(attrefid, OpenMode.ForWrite, false) as AttributeReference;
                                    switch (attref.Tag)
                                    {
                                        case "VIEWNO":
                                            prso = new PromptStringOptions("\nWhat view number is this?");
                                            res = ed.GetString(prso);
                                            if (res.Status == PromptStatus.OK)
                                            {
                                                attref.UpgradeOpen();
                                                string update = res.StringResult;
                                                attref.TextString = update;
                                                attref.DowngradeOpen();
                                            }
                                            break;
                                        case "FULLORPARTIAL":
                                            prko = new PromptKeywordOptions("\nWhat floor does this view depict?");
                                            prko.Keywords.Add("FULL");
                                            prko.Keywords.Add("PARTIAL");
                                            prko.Keywords.Add("DC");
                                            prko.Keywords.Add("ELEVATION");
                                            res = ed.GetKeywords(prko);
                                            if (res.Status == PromptStatus.OK)
                                            {
                                                attref.UpgradeOpen();
                                                string update = res.StringResult;
                                                attref.TextString = update;
                                                attref.DowngradeOpen();
                                            }
                                            break;
                                        case "FLOOR":
                                            prko = new PromptKeywordOptions("\nWhat floor does this view depict?");
                                            prko.Keywords.Add("GROUND");
                                            prko.Keywords.Add("FIRST");
                                            prko.Keywords.Add("SECOND");
                                            prko.Keywords.Add("THIRD");
                                            prko.Keywords.Add("FOURTH");
                                            prko.Keywords.Add("OTHER");
                                            PromptResult getWhichFloorResult = ed.GetKeywords(prko);
                                            if (getWhichFloorResult.StringResult == "OTHER")
                                            {
                                                res = ed.GetString("\nEnter another floor that's not listed.");
                                            }
                                            else
                                            {
                                                res = ed.GetKeywords(prko);
                                            }
                                            if (res.Status == PromptStatus.OK)
                                            {
                                                attref.UpgradeOpen();
                                                string update = res.StringResult;
                                                attref.TextString = update;
                                                attref.DowngradeOpen();
                                            }
                                            break;
                                    }
                                }
                                btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                                frameref = new BlockReference(BlockPos, bt["ISPVIEWPORT-FRAME"]);
                                frameref.ScaleFactors = BlockScale;
                                frameref.Rotation = BlockAngle;
                                btr.AppendEntity(frameref);
                                tr.AddNewlyCreatedDBObject(frameref, true);
                            }
                        }
                    }
                    tr.Commit();
                }
#endregion //InsertISPViewportframe
            }
        }

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8773
  • AKA Daniel
Re: Layout creation tool
« Reply #5 on: August 11, 2008, 09:50:11 AM »
I noticed you are reusing the variable btr, I don’t know if that might cause any problems

vegbruiser

  • Guest
Re: Layout creation tool
« Reply #6 on: August 11, 2008, 10:23:19 AM »
I noticed you are reusing the variable btr, I don’t know if that might cause any problems
Indeed I am :| . I'll call the two BlockTableRecords something else.

vegbruiser

  • Guest
Re: Layout creation tool
« Reply #7 on: August 12, 2008, 12:10:19 PM »
Another day, another problem.

Code: [Select]
public void InsertBlock(ObjectId Blockid, int ISPOSP)
        {
            //this assumes that because the viewport block has been copied to the drawing, the other necessary blocks will have been too.
            // Blockid = the id of the block we inserted using the jig.
            // BlockPos = the position of the above block.
            // Blockscale = the scale of the above block.
            // BlockAngle = the rotation angle of the above block.
            Database db = HostApplicationServices.WorkingDatabase;
            Document dwg = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Editor ed = dwg.Editor;
            BlockTable bt = null;
            BlockTableRecord btr = null;
            Point3d BlockPos = new Point3d(0, 0, 0);
            Scale3d BlockScale = new Scale3d(1);
            Double BlockAngle = 0;
            BlockReference blkref = null;
            BlockReference frameref = null;
            PromptStringOptions prso = null;
            PromptKeywordOptions prko = null;
            PromptResult res = null;
 
            // this next section adds the ISPviewport-frame block on top of the already-inserted ISPViewport block.
            #region InsertISPViewportframe
            if (ISPOSP == 0) // Viewport = ISP
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead, false) as BlockTable;
                    foreach (ObjectId btrid in bt)
                    {
                        if (btrid == Blockid)
                        // we found the correct block?
                        {
                            btr = tr.GetObject(btrid, OpenMode.ForRead, false) as BlockTableRecord;
                            // normally we could filter out layouts, xrefs etc. but since we already know the objectid of the block
                            // we're looking for we don't need to bother.
                            if (btr.IsLayout || btr.IsFromExternalReference || !btr.IsDynamicBlock || btr.IsFromOverlayReference || !btr.HasAttributeDefinitions)
                            {
                                continue;
                            }
                            ObjectIdCollection blkrefIds = btr.GetBlockReferenceIds(true, false);
                            if (blkrefIds == null || blkrefIds.Count == 0)
                                break;
                            //int counter = 0;
                            foreach (ObjectId blkrefid in blkrefIds)
                            {
                                blkref = tr.GetObject(blkrefid, OpenMode.ForWrite, false) as BlockReference;
                                BlockPos = blkref.Position;
                                BlockScale = blkref.ScaleFactors;
                                BlockAngle = blkref.Rotation;
                                // Code should skip the next part if the block has already been inserted and has a blockref.AttributeCollection.Count greater than zero.                               
                                if (blkref.AttributeCollection.Count == 0)
                                {
                                    //Start newly placed attribute start code.
                                    //Maybe need to filter so this only runs once.
                                    BlockTable newbt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                                    ObjectId bdid = bt[blkref.Name];
                                    BlockTableRecord newbtr = (BlockTableRecord)tr.GetObject(bdid, OpenMode.ForRead);
                                    //Add the attributes to the block definition otherwise we won't be able to edit them in blkref?
                                    if (newbtr.HasAttributeDefinitions)
                                    {
                                        foreach (ObjectId attid in newbtr)
                                        {
                                            AttributeDefinition attdef = tr.GetObject(attid, OpenMode.ForRead) as AttributeDefinition;
                                            if (attdef != null)
                                            {
                                                AttributeReference ar = new AttributeReference();
                                                ar.SetAttributeFromBlock(attdef, blkref.BlockTransform);
                                                blkref.AttributeCollection.AppendAttribute(ar);
                                                tr.AddNewlyCreatedDBObject(ar, true);
                                            }
                                        }
                                    }
                                    OldBlockPos = BlockPos;
                                }
                                blkref = tr.GetObject(blkrefid, OpenMode.ForRead, false) as BlockReference;
                                AttributeCollection attrefids = blkref.AttributeCollection;
                                foreach (ObjectId attrefid in attrefids)
                                {
                                    AttributeReference attref = tr.GetObject(attrefid, OpenMode.ForWrite, false) as AttributeReference;
                                    switch (attref.Tag)
                                    {
                                        case "VIEWNO_NONVIS":
                                            prso = new PromptStringOptions("\nWhat view number is this?");
                                            res = ed.GetString(prso);
                                            if (res.Status == PromptStatus.OK)
                                            {
                                                attref.UpgradeOpen();
                                                string update = res.StringResult;
                                                attref.TextString = update;
                                                attref.DowngradeOpen();
                                            }
                                            break;
                                        case "FULLORPARTIAL":
                                            prko = new PromptKeywordOptions("\nWhat floor does this view depict?");
                                            prko.Keywords.Add("FULL");
                                            prko.Keywords.Add("PARTIAL");
                                            prko.Keywords.Add("DC");
                                            prko.Keywords.Add("ELEVATION");
                                            res = ed.GetKeywords(prko);
                                            if (res.Status == PromptStatus.OK)
                                            {
                                                attref.UpgradeOpen();
                                                string update = res.StringResult;
                                                attref.TextString = update;
                                                attref.DowngradeOpen();
                                                if (res.StringResult == "PARTIAL")
                                                {
                                                    btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                                                    frameref = new BlockReference(BlockPos, bt["ISPVIEWPORT-FRAME"]);
                                                    frameref.ScaleFactors = BlockScale;
                                                    frameref.Rotation = BlockAngle;
                                                    btr.AppendEntity(frameref);
                                                    tr.AddNewlyCreatedDBObject(frameref, true);
                                                }
                                            }
                                            break;
                                        case "FLOOR":
                                            prko = new PromptKeywordOptions("\nWhat floor does this view depict?");
                                            prko.Keywords.Add("GROUND");
                                            prko.Keywords.Add("FIRST");
                                            prko.Keywords.Add("SECOND");
                                            prko.Keywords.Add("THIRD");
                                            prko.Keywords.Add("FOURTH");
                                            prko.Keywords.Add("OTHER");
                                            PromptResult getWhichFloorResult = ed.GetKeywords(prko);
                                            if (getWhichFloorResult.StringResult == "OTHER")
                                            {
                                                res = ed.GetString("\nEnter another floor that's not listed.");
                                            }
                                            else
                                            {
                                                res = getWhichFloorResult;
                                            }
                                            if (res.Status == PromptStatus.OK)
                                            {
                                                attref.UpgradeOpen();
                                                string update = res.StringResult;
                                                attref.TextString = update;
                                                attref.DowngradeOpen();
                                            }
                                            break;
                                    }
                                }
                            }
                        }
                    }
                    tr.Commit();
                }
            #endregion //InsertISPViewportframe
            }
        }

The above code allows me to insert the block(s) I require but for some reason it seems to go back into the previous inserted blocks and add the attributes again.

The line that isn't working as expected is: -

 if (blkref.AttributeCollection.Count == 0)

After the first block is inserted, and the attributes filled out, it shouldn't then have an AttributeCollection count of 0, but for reasons that escape me it does.  I can't for the life of me work out another check I could do to prevent the attributes being added again (short of moving the attribute editing part of this code into its' own function).

Any ideas?

(Incidentally, if I was doing this exact same thing in VBA I'd insert the block, then sendcommand "ATTSYNC" to sychronize the attributereferences with the blockdefinition.)
« Last Edit: August 12, 2008, 12:24:43 PM by vegbruiser »

vegbruiser

  • Guest
Re: Layout creation tool
« Reply #8 on: August 16, 2008, 06:01:49 PM »
Right, after a few more days spent reading through sample code (for dynamic blocks this time), this is what I've got: -
(see attached)

This code allows you to insert a Viewport block at any 2d location/angle you like, then pick what resultant sheet size you'd like: A0/A1/A2/A3 and what scale you want the viewport to be.

(The size/shape of the Viewport block is directly proportional to the available space in the template we use at any given scale - so if you were to choose A3 & a scale of 1:100 the final view will fit exactly on an A3 sheet :) .)

Next you input what sheet number this view is. (I plan on implementing the same counting facility I used in the VBA version of this code next week).

Then you have to decide what type of view it is - for my purposes the choices are Full/Partial/Data Closet/Elevation - but these can easily be changed to suit.

And that's it for now.

The next thing for me to do is put each new Viewport block onto a specific (in some cases unique) layer depending on the choice of view type - this then allows me to properly create the keyplans that are necessary for our designs.

After that my next step is to add a new named UCS for each of the new Viewport blocks - hence the as yet unused UCS Class. - This will then allow me to align the main Viewport in each resultant page to the new UCS.

Last but by no means least it's simply a matter of sorting an array of the Viewport numbers (because they'd have to be taken from each block in turn?) and starting to create the new layouts.

(if it doesn't drive me :loco: first that is) - that said, I am learning a lot though.

vegbruiser

  • Guest
Re: Layout creation tool
« Reply #9 on: August 20, 2008, 06:14:42 AM »
And for my next question: -

In VBA if I want to align a PaperSpace Viewport to a named UCS, I used to use the PLAN command - is there an equivalent function in .NET?

(I guess I will need to use XDATA stored in the viewport?)

EDIT: Actually, this might be what I need: -
(from here)
Code: [Select]
Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;

ViewTableRecord viewRec = ed.GetCurrentView();
Vector3d viewDirection = viewRec.ViewDirection;

Vector3d xVec = viewDirection.GetPerpendicularVector();
Vector3d yVec = xVec.CrossProduct(viewDirection.Negate());

Matrix3d mat = Matrix3d.AlignCoordinateSystem(
Point3d.Origin, Vector3d.XAxis, Vector3d.YAxis, Vector3d.ZAxis,
viewRec.Target, xVec, yVec, viewDirection.Negate());

//ed.CurrentUserCoordinateSystem = Matrix3d.Identity;
ed.CurrentUserCoordinateSystem = mat;
ed.UpdateScreen();
« Last Edit: August 20, 2008, 08:24:46 AM by vegbruiser »

vegbruiser

  • Guest
Re: Layout creation tool
« Reply #10 on: August 22, 2008, 10:57:55 AM »
One of the steps required by my Layout creation tool is the ability to store the layer properties that are present prior to running the code, and then restoring them afterwards.

The reason for this is that we have large drawings which, when the code was in vba would grind to a halt switching between layouts with all layers turned on/thawed. By freezing them/turning them off and then creating the layouts, I got around this problem. I would then simply reverse the changes with the exception of any newly created layers.

My problem now is that I'm a bit puzzled as to the best way to store the properties: -

Do I use an ArrayList, or an Array, or a Collection?

(I've searched TheSwamp and the Autodesk discussion groups archive I have on my (Non-Internet) machine but maybe I'm not looking for the right search string - the way I figure it I can't be the first person to want to store layer properties and subsequently restore them.)

Can anyone help?

Glenn R

  • Guest
Re: Layout creation tool
« Reply #11 on: August 22, 2008, 12:33:10 PM »
Can you explain further? Are you wanting to store WHILST the routine does it's creation thing, then restore the previous properties at the end or something else?

Also, when you said it ground to halt when switching layouts, what was your LAYOUTREGENCTL (fairly sure that's the variable name) setting?

vegbruiser

  • Guest
Re: Layout creation tool
« Reply #12 on: August 22, 2008, 04:12:18 PM »
Can you explain further? Are you wanting to store WHILST the routine does it's creation thing, then restore the previous properties at the end or something else?

Also, when you said it ground to halt when switching layouts, what was your LAYOUTREGENCTL (fairly sure that's the variable name) setting?
I would like to be able to store the layer settings of existing layers, then freeze everything except a specific few layers, run the creation tool, then finally restore the previously saved properties.

As for the LAYOUTREGENCTL variable, I have no idea. (The last time I had the problem it was using Autocad 2006 - I've not experienced it with AC2009.)

Glenn R

  • Guest
Re: Layout creation tool
« Reply #13 on: August 22, 2008, 04:35:22 PM »
From the AutoCAD 2006 help:

Code: [Select]
Type:  Integer
Saved in:  Registry
Initial value:  2


Specifies how the display list is updated in the Model tab and layout tabs. For each tab, the display list is updated either by regenerating the drawing when you switch to that tab or by saving the display list to memory and regenerating only the modified objects when you switch to that tab. Changing the LAYOUTREGENCTL setting can improve performance.

0
 The drawing is regenerated each time you switch tabs.
 
1
 For the Model tab and the last layout made current, the display list is saved to memory and regenerations are suppressed when you switch between the two tabs. For all other layouts, regenerations still occur when you switch to those tabs.
 
2
 The drawing is regenerated the first time you switch to each tab. For the remainder of the drawing session, the display list is saved to memory and regenerations are suppressed when you switch to those tabs.
 

The performance gain achieved by changing the LAYOUTREGENCTL setting is dependent on several factors, including the drawing size and type, the objects contained in the drawing, the amount of available memory, and the effect of other open drawings or applications. When LAYOUTREGENCTL is set to 1 or 2, the amount of additional memory used is the size of the Model tab’s display list multiplied by the number of viewports in each layout for which the display list is saved.

If LAYOUTREGENCTL is set to 1 or 2 and performance seems slow in general or when you switch between tabs for which the display list is saved, consider changing to a setting of 0 or 1 to find the optimal balance for your work environment. For additional information about performance settings, see System tab (in the Options dialog box).

Regardless of the LAYOUTREGENCTL setting, if you redefine a block or undo a tab switch, the drawing is regenerated the first time you switch to any tab that contains saved viewports.


You've already mentioned the drg's are complex/large - how many layout tabs are generally in each drawing?

vegbruiser

  • Guest
Re: Layout creation tool
« Reply #14 on: August 22, 2008, 04:45:13 PM »
It really depends on the drawing. That sounds obvious, but for some jobs there will likely be a maximum of three, but for more complex jobs we sometimes have over 100.

One thing that badly affects layout switching is the number of Viewports present on each layout. On a typical (standard) drawing we'll have no more than 7 viewports. So, on a large (100 layout) drawing that makes 700 Viewports.