.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Edit multiple drawings -Enotaplicable

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
kresimir.kukec
550 Views, 4 Replies

Edit multiple drawings -Enotaplicable

Hi

 

I am  trying to edit multiple drawings by changing DBtext i MText objects.

tekstovi lt = new tekstovi();
            Double pov = (Double)numericUpDown1.Value;
            Double ps = (Double)numericUpDown2.Value;
            if (true)
            {
                System.IO.StreamWriter sw = new System.IO.StreamWriter(plf+"logzamjene.txt");
                foreach (string put in filepathss)
                {
                    try
                    {
                        doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.Open(put.Replace("\\\\","\\"),false);
                    }
                    catch (System.Runtime.InteropServices.COMException ex)
                    {
                        System.Windows.Forms.MessageBox.Show(ex.Message);
                        throw;
                    }
                    Database db = doc.Database;
                        Editor ed = doc.Editor;
                    DocumentLock lok = doc.LockDocument();
                    using (lok)
                    {
                        using (Transaction tr = db.TransactionManager.StartTransaction())
                        {
                            try
                            {
                                SelectionFilter filter = new SelectionFilter(new TypedValue[] { new TypedValue(0, "TEXT,MTEXT") });
                                PromptSelectionResult selRes = ed.SelectAll(filter);
                                if (selRes.Status != PromptStatus.OK)
                                {
                                    MessageBox.Show("\nerror in getting the selectAll");
                                    return;
                                }
                                else
                                {
                                    foreach (SelectedObject so in selRes.Value)
                                    {
                                        DBObject ob = tr.GetObject(so.ObjectId, OpenMode.ForWrite);
                                        bool neispravno = false;
                                        DBText dbt = ob as DBText;
                                        MText mt = ob as MText;
                                        if (dbt != null)
                                        {
                                            String t = dbt.TextString;
                                            if (t.Contains("+"))
                                            {
                                                Double bb;
                                                tekst tt;
                                                if (Double.TryParse(t.Replace(",", ".").Replace("+", ""), out bb))
                                                {
                                                    if (bb > ps)
                                                    {
                                                        Double bbb = bb + pov;
                                                        tt = new tekst(bbb, bb, so.ObjectId, t);
                                                        lt.dodaj(tt);
                                                        dbt.TextString = bbb.ToString("0+000.00");
                                                        neispravno = false;
                                                    }
                                                    else
                                                    {
                                                        neispravno = true;
                                                    }
                                                }
                                                else
                                                {
                                                    neispravno = true;
                                                }
                                                if (neispravno)
                                                {
                                                    tt = new tekst(0, 0, so.ObjectId, t);
                                                }
                                            }
                                            else
                                            {
                                                tekst tt = new tekst(0, 0, dbt.ObjectId, t);
                                            }
                                        }
                                        else if (mt != null)
                                        {
                                            String t = mt.Contents;
                                            if (t.Contains("+"))
                                            {
                                                Double bb;
                                                tekst tt;
                                                if (Double.TryParse(t.Replace(",", ".").Replace("+", ""), out bb))
                                                {
                                                    if (bb > ps)
                                                    {
                                                        Double bbb = bb + pov;
                                                        tt = new tekst(bbb, bb, so.ObjectId, t);
                                                        lt.dodaj(tt);
                                                        mt.Contents = bbb.ToString("0+000.00");
                                                        neispravno = false;
                                                    }
                                                    else
                                                    {
                                                        neispravno = true;
                                                    }
                                                }
                                                else
                                                {
                                                    neispravno = true;
                                                }
                                                if (neispravno)
                                                {
                                                    tt = new tekst(0, 0, so.ObjectId, t);
                                                }
                                            }
                                            else
                                            {
                                                tekst tt = new tekst(0, 0, dbt.ObjectId, t);
                                            }
                                            /////////////////////////////////////////////////////////////////////////////////////////////////
                                        }
                                    }
                                }
                                lt.ispisi(sw);
                                sw.Close();
                                sw.Dispose();
                            }
                            catch (System.Exception ex)
                            {
                                System.Windows.Forms.MessageBox.Show(ex.Message);
                                tr.Abort();
                                sw.Close();
                                sw.Dispose();
                            }
                            tr.Commit();
                        }
                    }
                        doc.CloseAndSave(put);
                        //db.SaveAs(db.OriginalFileName, true, db.OriginalFileVersion, db.SecurityParameters);
                        
                    //}
                }
                sw.Close();
                sw.Dispose();
            }
        }
    }
}

 

Error came on line: 

PromptSelectionResult selRes = ed.SelectAll(filter);
4 REPLIES 4
Message 2 of 5

How do you run your code? I mean the CommandMethod, from which the code get run.

 

Basically,you need to set CommandFlags.Session in the CommandMethod attribute.

 

the Editor.SelectAll()/Selectxxxx() method can only be called with current drawing.

 

If the commandmethod has the Session flag, newly opened drawing will become current automatically. If there is no Session flag, drawing can be opened, but it would not become active drawing, thus you cannot use its Editor object and its method (SelectAll(), in your case), thus the "eNotApplicable" exception.

 

In following code, the Editor.SelectAll() works for me (Acad2015), with the CommandFlags.Session set:

 

 

    public class MyCadCommands
    {
        
        [CommandMethod("OpenDwgs", CommandFlags.Session)]
        public static void RunOpenDwgsCommand()
        {
            Document dwg = CadApp.DocumentManager.MdiActiveDocument;
            if (dwg == null) return;

            Editor ed = dwg.Editor;

            string[] files = new string[]
            {
                @"C:\Temp\DrawingTest1.dwg",
                @"C:\Temp\DrawingTest2.dwg"

                
            };

            BatchOpenDwgs(files);
            
        }

        private static void BatchOpenDwgs(string[] files)
        {
            foreach (var file in files)
            {
                Document dwg = Application.DocumentManager.Open(file, false);
                Editor ed = dwg.Editor;
                PromptSelectionResult res = ed.SelectAll();
                if (res.Status==PromptStatus.OK)
                {
                    Application.ShowAlertDialog(res.Value.Count + " found!");
                }
            }
        }
    }

 

 

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 5

Hi

 

I modified Class as you suggested with session flag but still no luck.

 

What am I doing wrong?

[CommandMethod("istac2", CommandFlags.Session)]
public static void istac2() // This method can have any name
    {
        System.Windows.Forms.OpenFileDialog openFileDialog2 = new System.Windows.Forms.OpenFileDialog();
        openFileDialog2.InitialDirectory = "E:\\SkyDrive\\Izvedbeni plinovod Međimurje\\uzduznjaci\\test\\";
        openFileDialog2.Filter = "dwg files (*.dwg)|*.dwg";
        openFileDialog2.FilterIndex = 1;
        openFileDialog2.RestoreDirectory = true;
        openFileDialog2.Title = "Odaberi datoteke";
        Double ps = 100;
        Double pov = 1500;
        if (openFileDialog2.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
			System.IO.StreamWriter sw = new System.IO.StreamWriter("E:\\SkyDrive\\Izvedbeni plinovod Međimurje\\uzduznjaci\\test\\rezultat.txt");
            foreach (string fn in openFileDialog2.FileNames)
            {
                tekstovi tt = changest(fn, ps, pov);
                tt.ispisi(sw);
            }
        }
    }
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////

 public static tekstovi changest(string path, Double ps, Double pov)
{
    tekstovi lt = new tekstovi();
    try
    {
        Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.Open(path.Replace("\\\\", "\\"), false);
        Database db = doc.Database;
        Editor ed = doc.Editor;
		SelectionFilter filter = new SelectionFilter(new TypedValue[] { new TypedValue(0, "TEXT,MTEXT") });
        PromptSelectionResult selRes = ed.SelectAll(filter);
        if (selRes.Status != PromptStatus.OK)
        {
			System.Windows.Forms.MessageBox.Show("\nerror in getting the selectAll");
            lt.dodaj(new tekst(0, 0, ObjectId.Null, ""));
        }
        else
        {
            foreach (SelectedObject so in selRes.Value)
            {
				using (Transaction tr = db.TransactionManager.StartTransaction())
				{
					DBObject ob = tr.GetObject(so.ObjectId, OpenMode.ForWrite);
					bool neispravno = false;
					DBText dbt = ob as DBText;
					MText mt = ob as MText;
					if (dbt != null)
					{
						String t = dbt.TextString;
						if (t.Contains("+"))
						{
							Double bb;
							if (Double.TryParse(t.Replace(",", ".").Replace("+", ""), out bb))
							{
								if (bb > ps)
								{
									Double bbb = bb + pov;
									lt.dodaj(new tekst(bbb, bb, so.ObjectId, t));
									dbt.TextString = bbb.ToString("0+000.00");
									neispravno = false;
								}
								else
								 {
									neispravno = true;
								}
							}
							else
							{
								neispravno = true;
							}
							if (neispravno)
							{
								lt.dodaj(new tekst(0, 0, so.ObjectId, t));
							}
						}
						else
						{
							lt.dodaj( new tekst(0, 0, dbt.ObjectId, t));
						}
					}
					else
					{
						String t = mt.Contents;
						if (t.Contains("+"))
							{
								Double bb;
								if (Double.TryParse(t.Replace(",", ".").Replace("+", ""), out bb))
								{
									if (bb > ps)
									{
										Double bbb = bb + pov;
										lt.dodaj(new tekst(bbb, bb, so.ObjectId, t));
										mt.Contents = bbb.ToString("0+000.00");
										neispravno = false;
									}
									else
									{
										neispravno = true;
									}
								}
								else
								{
									neispravno = true;
								}
								if (neispravno)
								{
									lt.dodaj(new tekst(0, 0, so.ObjectId, t));
								}
							}
							else
							{
								lt.dodaj( new tekst(0, 0, dbt.ObjectId, t));
							}
					}
					tr.Commit();

				}
			}
        }
                    
    }
    catch (System.Runtime.InteropServices.COMException ex)
    {
        System.Windows.Forms.MessageBox.Show(ex.Message);
        return new tekstovi();
    }
        return lt;
}

 

 

Message 4 of 5
moogalm
in reply to: kresimir.kukec

 

Hi Kresimir,

 

If you wan't to select either TEXT or MTEXT entities ,your filter should be something like this -

 

TypedValue[] TypVal = new TypedValue[4];
SelectionFilter filter= default(SelectionFilter);
TypVal.SetValue(new TypedValue(DxfCode.Operator, "<or"), 0);
TypVal.SetValue(new TypedValue(DxfCode.Start, "TEXT"), 1);
TypVal.SetValue(new TypedValue(DxfCode.Start, "MTEXT"), 2);
TypVal.SetValue(new TypedValue(DxfCode.Operator, "or>"), 3);
filter= new SelectionFilter(TypVal);

 

You stil find difficulties , I request you to send a sample project so I can analyze the issue.

 

Thanks,

madhu

 

 

 

Message 5 of 5

Which version of AutoCAD you are using?

 

Can you try some simplified code to just prove it does work or not work, for example the code I showed, or your code, from which you remove all the irrelevant code (only leave bare bone code to open a few drawings and call Editor.SelectAll() without filter?

Norman Yuan

Drive CAD With Code

EESignature

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost