AutoCAD 2014, C# call AddHatch throw exception

AutoCAD 2014, C# call AddHatch throw exception

Anonymous
Not applicable
1,048 Views
2 Replies
Message 1 of 3

AutoCAD 2014, C# call AddHatch throw exception

Anonymous
Not applicable

 

I use Auto CAD 2014, by in the follow code throw exception error,  I donot why, pls help me

 

private void button4_Click(object sender, EventArgs e)
{
AcadApplication app = new AcadApplicationClass();
app.Documents.Open(this.textBox1.Text, missing, missing);
app.Visible = false;

IAcadHatch hatch = app.ActiveDocument.ModelSpace.AddHatch(1, "ANSI31", true);
// inner loop
object[] inloop = new object[1];
AcadCircle circle = app.ActiveDocument.ModelSpace.AddCircle(new double[] { 100, 100, 0 }, 200);
inloop[0] = circle;
hatch.AppendInnerLoop(inloop);  // <====== in here: Invalid object array

 

string file = string.Format(@"{0}Demo_{1}.dwg", AppDomain.CurrentDomain.BaseDirectory, System.Guid.NewGuid());
app.ActiveDocument.SaveAs(file, AcSaveAsType.ac2010_dwg, missing);
app.Quit();
}

0 Likes
1,049 Views
2 Replies
Replies (2)
Message 2 of 3

norman.yuan
Mentor
Mentor

The AcadHatch.AppendInner[Outer]Loop() method takes an OBJECT (Variant in COM world), not an OBJECT ARRAY as input. That is:

 

object inLoop=new XXXXX[]{xxx, xxx, xxx};

 

is different from

 

object[] inLoop=new XXXXX[]{xxx, xxx, xxx};

 

So, this code works OK:

 

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;
using CadDb = Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Interop.Common;

[assembly: CommandClass(typeof(ComHatchTest.Commands))]

namespace ComHatchTest
{
    public class Commands 
    {
        [CommandMethod("MyHatch")]
        public static void DoDocumentCommand()
        {
            Document dwg = CadApp.DocumentManager.MdiActiveDocument;
            Editor ed = dwg.Editor;

            try
            {
                DoHatch();
            }
            catch (Autodesk.AutoCAD.Runtime.Exception aex)
            {
                ed.WriteMessage("\nError: {0}", aex.Message);
                ed.WriteMessage("\n*Cancel*");
            }
            catch (System.Exception ex)
            {
                ed.WriteMessage("\nError: {0}", ex.Message);
                ed.WriteMessage("\n*Cancel*");
            }
            finally
            {
                Autodesk.AutoCAD.Internal.Utils.PostCommandPrompt();
            }
        }

        public static void DoHatch()
        {
            AcadApplication app = CadApp.AcadApplication as AcadApplication;
            AcadDocument doc = app.ActiveDocument;

            AcadHatch hatch = doc.ModelSpace.AddHatch(1, "ANSI31", true);

            AcadCircle circle = doc.ModelSpace.AddCircle(new double[] { 100.0, 100.0, 0.0 }, 200.0);

            object inLoop = new AcadEntity[] { circle as AcadEntity };

            hatch.AppendOuterLoop(inLoop);
        }
    }
}

HTH

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 3

1605027848
Participant
Participant

I also Encountered the same problem. Then I corrected it follow your code.But when I define the elements of inLoop

as object,It still throws an exception.  Why? 


@norman.yuan wrote:

The AcadHatch.AppendInner[Outer]Loop() method takes an OBJECT (Variant in COM world), not an OBJECT ARRAY as input. That is:

 

object inLoop=new XXXXX[]{xxx, xxx, xxx};

 

is different from

 

object[] inLoop=new XXXXX[]{xxx, xxx, xxx};

 

So, this code works OK:

 

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;
using CadDb = Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Interop.Common;

[assembly: CommandClass(typeof(ComHatchTest.Commands))]

namespace ComHatchTest
{
    public class Commands 
    {
        [CommandMethod("MyHatch")]
        public static void DoDocumentCommand()
        {
            Document dwg = CadApp.DocumentManager.MdiActiveDocument;
            Editor ed = dwg.Editor;

            try
            {
                DoHatch();
            }
            catch (Autodesk.AutoCAD.Runtime.Exception aex)
            {
                ed.WriteMessage("\nError: {0}", aex.Message);
                ed.WriteMessage("\n*Cancel*");
            }
            catch (System.Exception ex)
            {
                ed.WriteMessage("\nError: {0}", ex.Message);
                ed.WriteMessage("\n*Cancel*");
            }
            finally
            {
                Autodesk.AutoCAD.Internal.Utils.PostCommandPrompt();
            }
        }

        public static void DoHatch()
        {
            AcadApplication app = CadApp.AcadApplication as AcadApplication;
            AcadDocument doc = app.ActiveDocument;

            AcadHatch hatch = doc.ModelSpace.AddHatch(1, "ANSI31", true);

            AcadCircle circle = doc.ModelSpace.AddCircle(new double[] { 100.0, 100.0, 0.0 }, 200.0);

            object inLoop = new AcadEntity[] { circle as AcadEntity };

            hatch.AppendOuterLoop(inLoop);
        }
    }
}

HTH


 

0 Likes