Author Topic: PromptStringOptions-->AllowSpaces: Am I missing Something?  (Read 3838 times)

0 Members and 1 Guest are viewing this topic.

wannabe

  • Guest
PromptStringOptions-->AllowSpaces: Am I missing Something?
« on: April 30, 2009, 04:30:38 AM »
When I run the following code, AutoCAD is still accepting space-bar input as an instruction to complete input of my string.

Can anyone advise if I am missing a fundamental, please?

Quote
  PromptStringOptions psoSelect = new PromptStringOptions("Key-in Block Name or <Enter> to Select Wblock");
                psoSelect.AllowSpaces.Equals(false);
                PromptResult prBlockSelected = ed.GetString(psoSelect);

Code: It says false but I have tried true.
« Last Edit: April 30, 2009, 04:46:24 AM by wannabe »

Tuoni

  • Gator
  • Posts: 3032
  • I do stuff, and things!

wannabe

  • Guest
Re: PromptStringOptions-->AllowSpaces: Am I missing Something?
« Reply #2 on: April 30, 2009, 04:51:07 AM »
Cheers for taking an interest, mate.

. I don't think that is my problem though. In the code I showed it says allow spaces as false That's because I tried it as true with zero luck. Well that's what I think. I'll be more sincere and report back.


Tuoni

  • Gator
  • Posts: 3032
  • I do stuff, and things!
Re: PromptStringOptions-->AllowSpaces: Am I missing Something?
« Reply #3 on: April 30, 2009, 05:07:44 AM »
It's because you're using .Equals() - That just returns a boolean depending on whether the object's contents equals the parameter you pass it.

Say if I have a string :

Code: [Select]
String str = "this is a string";
String str2 = "this is a string";

bool same = str.Equals(str2); //same is now true

str2 = "not the same now";

same = str.Equals(str2); //same is now false

Use the = operator to assign the value of true.

Glenn R

  • Guest
Re: PromptStringOptions-->AllowSpaces: Am I missing Something?
« Reply #4 on: April 30, 2009, 05:35:21 AM »
^^^ What he said.

wannabe

  • Guest
Re: PromptStringOptions-->AllowSpaces: Am I missing Something?
« Reply #5 on: April 30, 2009, 06:34:31 AM »
The initial attempt read psoSelect.AllowSpaces = true;

I've been faffing about trying to make it work and thus the code I copied is wrong. Take my word that with the AllowSpaces = true, when I use the spacebar it's acting as though I want to accept the current command line contents.

Code: [Select]
namespace MultiBlockClass
{
    public class Commands
    {
        [CommandMethod("MultiBlocks")]
        public void MultiBlocks()
        {
            Database sourceDb;
            bool wblock = false;
            Database db = HostApplicationServices.WorkingDatabase;

            OpenFileDialog coordsSelectDialog = getCoordsFile();

            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            string blockName = "";
            ObjectId blockId;
            bool isStringValid = false;//Enabling us to loop until string is valid or user cancels
            while (!isStringValid)
            {

                               PromptStringOptions psoSelect = new PromptStringOptions("Key-in Block Name or <Enter> to Select Wblock");
                psoSelect.AllowSpaces = true;
                PromptResult prBlockSelected = ed.GetString(psoSelect);

                if (prBlockSelected.Status == PromptStatus.Cancel)
                    return;

                else if (prBlockSelected.Status != PromptStatus.OK)
                    System.Windows.Forms.MessageBox.Show("Error With User Input - Try Again or Cancel", "Error");

                else if (!String.IsNullOrEmpty(prBlockSelected.StringResult))
                {
                    isStringValid = true;
                    blockName = prBlockSelected.StringResult;
                    break;//Database-resident "Block Reference" chosen - pending validation
                }

                isStringValid = true; //No more looping
                wblock = true;
            }
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);

                if (!wblock)//Search Block Table for database-resident Block Reference
                {
                    //Iterate the Block Table, searching for our user-specified "Block Reference"
                    if (!bt.Has(blockName.ToString()) && !wblock)
                    {
                        System.Windows.Forms.MessageBox.Show("Specified 'Block Reference' Not Valid in This Document - Application Terminated"
                                                             , "Input Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);

                        return;
                    }
                    blockId = bt[blockName.ToString()];//Essentially our "Block Definition"
                }

                else//We need to select an external DWG file as our insertable Wblock
                {
                    //Set a reference to the source database via an OpenFileDialog
                    sourceDb = new Database();

                    OpenFileDialog open = new OpenFileDialog("Select DWG", "Select File", "dwg",
                                                             "WBlock Selection", OpenFileDialog.OpenFileDialogFlags.NoUrls);
                    open.ShowDialog();

                    string filename = open.Filename;//Target database filename
                    sourceDb.ReadDwgFile(filename, FileShare.Read, true, null);//Source database "wrapped"
                    blockId = db.Insert("newBlock", sourceDb, true);
                }

                using (TextReader coordsFile = new StreamReader(coordsSelectDialog.Filename))
                {
                    while (coordsFile.Peek() > 0)//Until all lines of our stream have been read in
                    {
                        try
                        {
                            string lineRead = coordsFile.ReadLine();

                            if (String.IsNullOrEmpty(lineRead))//Shouldn't get to this stage, but just in-case...
                                break;

                           
                            string[] lineSeparated = lineRead.Trim().Split(' ');

                            //Convert first two elements to double for our insertion point
                            Point3d insPoint = new Point3d(Double.Parse(lineSeparated[0]), Double.Parse(lineSeparated[1]), 0);

                            BlockReference blockRef = new BlockReference(insPoint, blockId);

                            //Add to ModelSpace immediately to ensure successful attribute population
                            BlockTableRecord mSpace = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                            mSpace.AppendEntity(blockRef);
                            trans.AddNewlyCreatedDBObject(blockRef, true);

                            if (lineSeparated.Count() > 2)//If true, we have attribute data
                            {
                                BlockTableRecord thisBlock = (BlockTableRecord)trans.GetObject(blockId, OpenMode.ForRead);
                                int counter = 2; //Element in "lineSeparated" where attribute information will begin - after coordinates
                                foreach (ObjectId obj in thisBlock)
                                {
                                    Entity ent = (Entity)trans.GetObject(obj, OpenMode.ForRead);

                                    if (ent is AttributeDefinition)
                                    {
                                        AttributeDefinition attDef = (AttributeDefinition)(ent);

                                        AttributeReference attRef = new AttributeReference();
                                        attRef.SetPropertiesFrom(ent);
                                        attRef.TextString = lineSeparated[counter];
                                        attRef.Position = new Point3d(blockRef.Position.X + attDef.Position.X,
                                                                      blockRef.Position.Y + attDef.Position.Y,
                                                                      0);
                                        attRef.Tag = attDef.Tag;
                                        attRef.Height = attDef.Height;
                                        attRef.Rotation = attDef.Rotation;

                                        //Add this attribute to the "Block Reference" & the transaction
                                        blockRef.AttributeCollection.AppendAttribute(attRef);
                                        trans.AddNewlyCreatedDBObject(attRef, true);

                                        //Increment the counter so the next attribute will utilise the next array element
                                        counter++;
                                    }

                                }
                            }
                        }
                        catch
                        {
                            System.Windows.Forms.MessageBox.Show("Error - Application Terminated", "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                            return;
                        }
                    }
                }

                //Complete by adding Block Referenc to transaction
                trans.Commit();
            }
        }

        public OpenFileDialog getCoordsFile()
        {
            OpenFileDialog coordsSelectDialog = new OpenFileDialog("Coodinate Selection", "", "txt",
                                                       "Select a Coordinate File", OpenFileDialog.OpenFileDialogFlags.NoUrls);
            bool coordsSelected = false;//So we can loop until valid file selected or user cancels
            while (!coordsSelected)
            {
                System.Windows.Forms.DialogResult coordsResult = coordsSelectDialog.ShowDialog();

                if (coordsResult == System.Windows.Forms.DialogResult.Cancel)
                    return null;

                if (coordsResult != System.Windows.Forms.DialogResult.OK)
                    System.Windows.Forms.MessageBox.Show("Select Another File");

                else
                    coordsSelected = true; //Loop will no longer execute & we have a valid file
            }
            return coordsSelectDialog;
        }
    }
}
                 

           

If anyone can verify whether it works as expected on their machine it would be appreciated.

The text file will need to have values separated by a blank space and the block will need to be entered by typing it's name.

Glenn R

  • Guest
Re: PromptStringOptions-->AllowSpaces: Am I missing Something?
« Reply #6 on: April 30, 2009, 06:37:32 AM »
Actually, you want to assign the value of FALSE, as you do NOT want to AllowSpaces...if that's your intent.

.AllowSpaces = false;

Glenn R

  • Guest
Re: PromptStringOptions-->AllowSpaces: Am I missing Something?
« Reply #7 on: April 30, 2009, 06:40:44 AM »
Ignore that - I see you want to allow input where a space does not signify enter.

Tuoni

  • Gator
  • Posts: 3032
  • I do stuff, and things!
Re: PromptStringOptions-->AllowSpaces: Am I missing Something?
« Reply #8 on: April 30, 2009, 06:41:02 AM »
Actually, you want to assign the value of FALSE, as you do NOT want to AllowSpaces...if that's your intent.

.AllowSpaces = false;
AllowSpaces = false sets it so that you cannot have a space in your string.

AllowSpaces = true; means that you can have spaces in your string.

Also, I'm out - I don't have AutoCAD on here.

wannabe

  • Guest
Re: PromptStringOptions-->AllowSpaces: Am I missing Something?
« Reply #9 on: April 30, 2009, 06:51:33 AM »
Actually, you want to assign the value of FALSE, as you do NOT want to AllowSpaces...if that's your intent.

.AllowSpaces = false;
AllowSpaces = false sets it so that you cannot have a space in your string.

AllowSpaces = true; means that you can have spaces in your string.

Also, I'm out - I don't have AutoCAD on here.


Was that a Dragon's Den style "I'm out"?

Either way, cheers for your time.

Tuoni

  • Gator
  • Posts: 3032
  • I do stuff, and things!
Re: PromptStringOptions-->AllowSpaces: Am I missing Something?
« Reply #10 on: April 30, 2009, 06:54:43 AM »
Actually, you want to assign the value of FALSE, as you do NOT want to AllowSpaces...if that's your intent.

.AllowSpaces = false;
AllowSpaces = false sets it so that you cannot have a space in your string.

AllowSpaces = true; means that you can have spaces in your string.

Also, I'm out - I don't have AutoCAD on here.
Was that a Dragon's Den style "I'm out"?

Either way, cheers for your time.
Something like that :evil:

No problems.

wannabe

  • Guest
Re: PromptStringOptions-->AllowSpaces: Am I missing Something?
« Reply #11 on: April 30, 2009, 07:41:28 AM »
Ok, the code works fine :-D.

Basically, I transferred the code to my work machine, where I wasn't debugging via the option in Visual Studio; I was opening AutoCAD and loading the .DLL....only, I wasn't rebuilding each time....All I was doing was loading the original .DLL.

Lesson learnt I suppose, look on the bright side, every cloud etc.

Feel free to ridicule me :-D

Tuoni

  • Gator
  • Posts: 3032
  • I do stuff, and things!
Re: PromptStringOptions-->AllowSpaces: Am I missing Something?
« Reply #12 on: April 30, 2009, 07:54:48 AM »
:-D

It happens to the best of us, I'm sure.