c# Command just-in-time (JIT) with form

c# Command just-in-time (JIT) with form

naderlabbad309
Participant Participant
1,926 Views
14 Replies
Message 1 of 15

c# Command just-in-time (JIT) with form

naderlabbad309
Participant
Participant

Hello!

First sorry for the poor Title.  Anyway I'm making a Form for AutoCAD in C# So at the beginning I got error by creating polyline in case using Form

 

Application does not support Windows Forms just-in-time (JIT)

So I looked in the form and I found that I should lock the Document and it solved the problem 

 

 

 

 

 

 

 

using (DocumentLock acLckDoc = acDoc.LockDocument())

 

 

 

 

 

 

 But after that I had to use ed.Command to perform fillet like this .

 

 

 

 

 

 

 acDoc.Database.Filletrad = 5.0;
 ed.Command("_.fillet", "Polyline", acPoly.ObjectId);

 

 

 

 

 

 

And I got this error again but locking the Document didn't help this time

Application does not support Windows Forms just-in-time (JIT)

help me plz and thx in advance 

Nader.

 

 

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;

namespace AutoNader.Data.Creator
{
    public class PolyLineClass
    {
       
        public static void PolyLineCreate()
        {
            // Get the current document and database
            Document acDoc = Application.DocumentManager.MdiActiveDocument;         
            Database acCurDb = acDoc.Database;
            var ed = acDoc.Editor;
            Polyline acPoly = new Polyline();

            // Lock the new document
            using (DocumentLock acLckDoc = acDoc.LockDocument())
            {
                // Start a transaction
                using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
                {
                    // Open the Block table for read
                    BlockTable acBlkTbl;
                    acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                                                 OpenMode.ForRead) as BlockTable;

                    // Open the Block table record Model space for write
                    BlockTableRecord acBlkTblRec;
                    acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                                                    OpenMode.ForWrite) as BlockTableRecord;

                    // Create a polyline with two segments (3 points)
                   
                    
                    acPoly.SetDatabaseDefaults();
                    acPoly.AddVertexAt(0, new Point2d(0, 0), 0, 0, 0);
                    acPoly.AddVertexAt(1, new Point2d(300, 0), 0, 0, 0);
                    acPoly.AddVertexAt(2, new Point2d(300, 300), 0, 0, 0);
                    acPoly.AddVertexAt(3, new Point2d(0, 300), 0, 0, 0);
                    acPoly.AddVertexAt(4, new Point2d(0, 0), 0, 0, 0);

               

                    // Add the new object to the block table record and the transaction
                    acBlkTblRec.AppendEntity(acPoly);
                    acTrans.AddNewlyCreatedDBObject(acPoly, true);
                 
                    // Save the new object to the database
                    acTrans.Commit();
                    acDoc.Database.Filletrad = 5.0;
                    ed.Command("_.fillet", "Polyline", acPoly.ObjectId);




                }

            }
     
        }
    


    }
}

 

 

 

zK5AV.png

 

 

private void Button1_Click(object sender, EventArgs e)
        {
            PolyLineClass.PolyLineCreate();     
        }

 

    public class MainClass
    {
        [CommandMethod("AN")]
        public void Main()
        {
            Form1 F1 = new Form1();
            F1.Show();
         // PolyLineClass.PolyLineCreate();
         
        }

    }

 

0 Likes
Accepted solutions (1)
1,927 Views
14 Replies
Replies (14)
Message 2 of 15

norman.yuan
Mentor
Mentor

You should show more code of yours (e.g. the entire code execution context where the line of code causes error, rather than just the error message, which sometimes does not provide meaningful clue at all.

 

In your case, since you mention Form, you need to show how the code is shown in AutoCAD (modal, or modeless), how do you ca;; Editor.Command()....

 

Because you said that locking your document helped you, it implies that your form is modeless. If so, and you call Editor.Command() from a form event (clicking a button, for example), then, that is the issue: Editor.Command can only be called in document context, while a modeless form is in Application context. 

 

If you are fairly new, you need to know a lot more than very basics in order to know how/when to use modeless form.

 

Again, show entire chunk of related code, plus necessary descriptions, so that other can better understand what you want to do, and whether you do it correctly or not.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 15

naderlabbad309
Participant
Participant

I edit my post and include all the code line 58 59 if I delete 

acDoc.Database.Filletrad = 5.0;
ed.Command("_.fillet", "Polyline", acPoly.ObjectId); 

the code work well

0 Likes
Message 4 of 15

norman.yuan
Mentor
Mentor

Well, you still not show the code that is related to your issue: how the form is shown: is the form shown as modal or modeless. If it is the latter, that is the source of the problem: you cannot call Editor.Command() from modeless form/ApplicationContext, or from a command method that has CommandFlgas.Session bit set.

 

If you have to do this from a modeless form, you can use Document.SendTextToExecution(...) to execute an existing command.

 

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 5 of 15

naderlabbad309
Participant
Participant
the form called by MainClass
public class MainClass
{
[CommandMethod("AN")]
public void Main()
{
Form1 F1 = new Form1();
F1.Show();
// PolyLineClass.PolyLineCreate();

}

}
0 Likes
Message 6 of 15

naderlabbad309
Participant
Participant

so I should use this
acDoc.SendStringToExecute("_.fillet", true, false,false);

it worked but i should selects by mouse also choose Polyline and then the object 

0 Likes
Message 7 of 15

naderlabbad309
Participant
Participant
acDoc.SendStringToExecute("_.fillet ", true, false,true);
acDoc.SendStringToExecute("Polyline ", true, false, true);
acDoc.SendStringToExecute("Radius ", true, false, true);
acDoc.SendStringToExecute("5 ", true, false, true);
it worked but i can't put the poly line object so it done auto i should selected
0 Likes
Message 8 of 15

Alexander.Rivilis
Mentor
Mentor

Command _FILLET waiting not for ObjectId of object but for list of ObjectId of object and point of selection.

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

0 Likes
Message 9 of 15

naderlabbad309
Participant
Participant
so are there a way to select by ObjectId ?
0 Likes
Message 10 of 15

Alexander.Rivilis
Mentor
Mentor

Try code of @_gile

https://forums.autodesk.com/t5/net/how-to-apply-fillet-to-lwpolyline-with-c/m-p/8962763#M63442 

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

0 Likes
Message 11 of 15

norman.yuan
Mentor
Mentor
Accepted solution

You DO NOT show WinForm by calling

 

Form.Show()/ShowDialog();

 

You need to use

 

Application.ShowModalDialog()/ShowModelessDialog();

 

Using Application.ShowModelessDialog(), which shows the for in the same way as Form.Show() (which you should not use!), is a very tricky and is a bit advanced topic. The way you show the form in AutoCAD is quite problematic: try to execute your command "AN" multiple time, you would see what I mean.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 12 of 15

naderlabbad309
Participant
Participant
thx like this
Form1 F1 = new Form1();
Application.ShowModalDialog(null, F1, false);
0 Likes
Message 13 of 15

naderlabbad309
Participant
Participant
thx it worked with
acDoc.Database.Filletrad = 5.0;
ed.Command("_.fillet", "Polyline", acPoly.ObjectId);
it was the way how to call form i don't have a lot of experience with form if you can send to the right place for autocad form docm
0 Likes
Message 14 of 15

norman.yuan
Mentor
Mentor

If you have to do the work from a modeless form (well, as I said, you need to first understand how modeless form work in AutoCAD. typically, you would want to make it a singleton form in ApplicationContext), you can wrap Edtor.Command() in your own CommandMethod, and then use SendStringToExecute(0 to call your command. Something like:

 

// Do your customized fillet work 

[CommandMethod("MyFillet")]

public static void DoFillet()

{

    // Create polyline

    ...

    // Call Editor.Command here

    ed.Command("_.fillet", ......);

    // Or, you can simply show your math skill to calculate

    // the arc that collect each vetex point of the polyline

}

 

// Show the form

private static MyForm _form = null;

[CommandMethod("ShowForm", CommandFlags.Session]

public static void ShowUI()

{

    If (_form == null)

    {

        _form=new MyForm();

    }

    Application.ShowModelessDialog(_form);

}

 

// Then with a button's click event of the form

private void Button1_Click(...)

{

    var doc=Application....MdiActiveDocument;

    doc.SendStringToExecute("MyFillet ", ....);

}

 

Hope this give you an idea.

 

Norman Yuan

Drive CAD With Code

EESignature

Message 15 of 15

naderlabbad309
Participant
Participant
Thank you a lot it indeed help thx again
0 Likes