Author Topic: Attaching Images in Bricscad 12 via .NET Code  (Read 6562 times)

0 Members and 1 Guest are viewing this topic.

twdotson

  • Mosquito
  • Posts: 17
Attaching Images in Bricscad 12 via .NET Code
« on: March 22, 2012, 08:47:58 PM »
Anyone have an example of loading/inserting an image object in Bricscad .NET code?  I'm using code that has always worked well in WellKnownCAD but produces an "Attempted to read or write protected memory" error on the .Load method in BCAD (Teigha.DatabaseServices.RasterImageDef.Load).

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8659
  • AKA Daniel
Re: Attaching Images in Bricscad 12 via .NET Code
« Reply #1 on: March 22, 2012, 09:43:59 PM »
can you show your code?

twdotson

  • Mosquito
  • Posts: 17
Re: Attaching Images in Bricscad 12 via .NET Code
« Reply #2 on: March 23, 2012, 11:14:29 AM »
Not radically different that an example on this site:

http://www.theswamp.org/index.php?topic=31863.msg392684#msg392684

I used the above, commented the .ActiveFileName because it was returning "Not Implemented" and it also failed with the same error code.  I'm beginning to think there is a bug in the API ?

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8659
  • AKA Daniel
Re: Attaching Images in Bricscad 12 via .NET Code
« Reply #3 on: March 23, 2012, 10:48:31 PM »
try moving the load method after ImageDic.SetAt.

twdotson

  • Mosquito
  • Posts: 17
Re: Attaching Images in Bricscad 12 via .NET Code
« Reply #4 on: March 24, 2012, 04:38:30 PM »
That made the difference, thanks.

huiz

  • Swamp Rat
  • Posts: 913
  • Certified Prof C3D
Re: Attaching Images in Bricscad 12 via .NET Code
« Reply #5 on: November 27, 2012, 04:05:13 AM »
If I use the mentioned code in BricsCAD, the images are loaded and shown but the size is 0 in width and height. I also can't change this setting in the properties dialog.

In AutoCAD the code works well.

Anyone who has added images succesfully in BricsCAD?


The conclusion is justified that the initialization of the development of critical subsystem optimizes the probability of success to the development of the technical behavior over a given period.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8659
  • AKA Daniel
Re: Attaching Images in Bricscad 12 via .NET Code
« Reply #6 on: November 29, 2012, 05:04:26 AM »
this works for me

Code: [Select]
   [CommandMethod("doit")]
    static public void doit()
    {
      Document doc = Application.DocumentManager.MdiActiveDocument;
      Database db = doc.Database;
      using (Transaction tr = db.TransactionManager.StartTransaction())
      {
        ObjectId dictId = RasterImageDef.GetImageDictionary(db);
        if (dictId.IsNull)
          dictId = RasterImageDef.CreateImageDictionary(db);

        DBDictionary dict = tr.GetObject(dictId, OpenMode.ForWrite) as DBDictionary;
        RasterImageDef rid = new RasterImageDef();
        rid.SourceFileName = "c:\\Capture.tif";
        ObjectId defId = dict.SetAt("Capture", rid);
        rid.Load();
        tr.AddNewlyCreatedDBObject(rid, true);

        RasterImage ri = new RasterImage();
        ri.ImageDefId = defId;
        ri.ShowImage = true;
        ri.Orientation = new CoordinateSystem3d(Point3d.Origin,Vector3d.XAxis,Vector3d.YAxis);

        BlockTableRecord btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
        btr.AppendEntity(ri);
        tr.AddNewlyCreatedDBObject(ri, true);
        ri.AssociateRasterDef(rid);
        tr.Commit();
      }
    }
  }

Cheers

huiz

  • Swamp Rat
  • Posts: 913
  • Certified Prof C3D
Re: Attaching Images in Bricscad 12 via .NET Code
« Reply #7 on: November 29, 2012, 07:12:46 AM »
You do not scale the image in your code, that is the problem I face.

Code: [Select]
  RasterEnt = New RasterImage
  RasterEnt.SetDatabaseDefaults(ThisDoc.Database)
  RasterEnt.ShowImage = True
  Matrix = New Matrix3d
  Matrix = Matrix3d.Scaling(Scale / RasterDef.Size.X, New Point3d(0, 0, 0))
  RasterEnt.TransformBy(Matrix)

This does not seem to work in BricsCAD, but does in AutoCAD.
The conclusion is justified that the initialization of the development of critical subsystem optimizes the probability of success to the development of the technical behavior over a given period.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8659
  • AKA Daniel
Re: Attaching Images in Bricscad 12 via .NET Code
« Reply #8 on: November 29, 2012, 08:47:06 AM »
This one with scaling seems to work fine.

Code: [Select]
    [CommandMethod("doit")]
    static public void doit()
    {
      Document doc = Application.DocumentManager.MdiActiveDocument;
      Database db = doc.Database;
      using (Transaction tr = db.TransactionManager.StartTransaction())
      {
        ObjectId dictId = RasterImageDef.GetImageDictionary(db);
        if (dictId.IsNull)
          dictId = RasterImageDef.CreateImageDictionary(db);

        DBDictionary dict = tr.GetObject(dictId, OpenMode.ForWrite) as DBDictionary;
        RasterImageDef rid = new RasterImageDef();
        rid.SourceFileName = "c:\\Capture.tif";
        ObjectId defId = dict.SetAt("Capture", rid);
        rid.Load();
        tr.AddNewlyCreatedDBObject(rid, true);

        RasterImage ri = new RasterImage();
        ri.ImageDefId = defId;
        ri.ShowImage = true;
        ri.Orientation = new CoordinateSystem3d(Point3d.Origin, Vector3d.XAxis, Vector3d.YAxis);
        ri.TransformBy(Matrix3d.Scaling(3 / rid.Size.X, new Point3d(0, 0, 0)));

        BlockTableRecord btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
        btr.AppendEntity(ri);
        tr.AddNewlyCreatedDBObject(ri, true);
        ri.AssociateRasterDef(rid);
        tr.Commit();
      }
    }
« Last Edit: November 29, 2012, 08:54:10 AM by eNotEnoughBeer »

huiz

  • Swamp Rat
  • Posts: 913
  • Certified Prof C3D
Re: Attaching Images in Bricscad 12 via .NET Code
« Reply #9 on: November 29, 2012, 03:25:00 PM »
Unfortunately your code doesn't help me neither. If I use your function the images have both a width and height of 1, while they should not be square. At least it is better than 0, the result of the previous function, but still not good.

Thanks anyway for your help :-)
The conclusion is justified that the initialization of the development of critical subsystem optimizes the probability of success to the development of the technical behavior over a given period.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Attaching Images in Bricscad 12 via .NET Code
« Reply #10 on: November 29, 2012, 05:06:18 PM »
huiz,
if you debug and step through your ( Dans) code ; what us the value of rid.Size.X ??
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8659
  • AKA Daniel
Re: Attaching Images in Bricscad 12 via .NET Code
« Reply #11 on: November 29, 2012, 08:10:27 PM »
Unfortunately your code doesn't help me neither. If I use your function the images have both a width and height of 1, while they should not be square. At least it is better than 0, the result of the previous function, but still not good.

Thanks anyway for your help :-)

My code? I used your scaling scheme verbatim, well except in a more performant  C# version.  :lol:

Seriously though,  The default seems to be inserted with a uniform matrix, your you'll need to create a scaling matrix that fits your needs... I'll just use the image width and height for now, you might want to use a ratio or whatever.. I'll leave that up to you : )

Code: [Select]
[CommandMethod("doit")]
    static public void doit()
    {
      Document doc = Application.DocumentManager.MdiActiveDocument;
      Database db = doc.Database;
      using (Transaction tr = db.TransactionManager.StartTransaction())
      {
        ObjectId dictId = RasterImageDef.GetImageDictionary(db);
        if (dictId.IsNull)
          dictId = RasterImageDef.CreateImageDictionary(db);

        DBDictionary dict = tr.GetObject(dictId, OpenMode.ForWrite) as DBDictionary;
        RasterImageDef rid = new RasterImageDef();
        rid.SourceFileName = "c:\\Capture.tif";
        ObjectId defId = dict.SetAt("Capture", rid);
        rid.Load();
        tr.AddNewlyCreatedDBObject(rid, true);

        RasterImage ri = new RasterImage();
        ri.ImageDefId = defId;
        ri.ShowImage = true;

        ri.Orientation = new CoordinateSystem3d(Point3d.Origin, Vector3d.XAxis, Vector3d.YAxis);
        double[] _xform = new double[16];
        _xform[0] = rid.Size.X;
        _xform[5] = rid.Size.Y;
        _xform[10] = 1.0;
        _xform[15] = 1.0;
        Matrix3d smatrix = new Matrix3d(_xform);
        ri.TransformBy(smatrix);

        ri.TransformBy(Matrix3d.Scaling(3 , new Point3d(0, 0, 0)));
        ri.Orientation = new CoordinateSystem3d(ri.Orientation.Origin, ri.Orientation.Xaxis, ri.Orientation.Yaxis);


        BlockTableRecord btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
        btr.AppendEntity(ri);
        tr.AddNewlyCreatedDBObject(ri, true);
        ri.AssociateRasterDef(rid);
        tr.Commit();
      }
    }

huiz

  • Swamp Rat
  • Posts: 913
  • Certified Prof C3D
Re: Attaching Images in Bricscad 12 via .NET Code
« Reply #12 on: November 30, 2012, 02:07:17 PM »
Well, VB.Net reads good too :-)

Code: [Select]
  Public Sub AddRaster(RasterFile As String, InsPoint As Point3d, Scale As Double, Layer As String)
    Dim doc As acAppServ.Document = acAppServ.Application.DocumentManager.MdiActiveDocument
    Dim db As Database = doc.Database
    Using tr As Transaction = db.TransactionManager.StartTransaction()
      Dim dictId As ObjectId = RasterImageDef.GetImageDictionary(db)
      If dictId.IsNull Then
        dictId = RasterImageDef.CreateImageDictionary(db)
      End If

      ' Filename from path.
      Dim RasterName As String = RasterFile.Substring(RasterFile.LastIndexOf("\") + 1)
      RasterName = RasterName.Substring(0, RasterName.IndexOf("."))

      Dim dict As DBDictionary = TryCast(tr.GetObject(dictId, OpenMode.ForWrite), DBDictionary)
      Dim rid As New RasterImageDef()
      rid.SourceFileName = RasterFile
      Dim defId As ObjectId = dict.SetAt(RasterName, rid)
      rid.Load()
      tr.AddNewlyCreatedDBObject(rid, True)

      Dim ri As New RasterImage()
      ri.ImageDefId = defId
      ri.ShowImage = True

      ri.TransformBy(Matrix3d.Scaling(Scale / rid.Size.X, New Point3d(0, 0, 0)))
      ri.Orientation = New CoordinateSystem3d(InsPoint, Vector3d.XAxis, Vector3d.YAxis)

      Dim btr As BlockTableRecord = TryCast(tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite), BlockTableRecord)
      btr.AppendEntity(ri)
      tr.AddNewlyCreatedDBObject(ri, True)
      ri.AssociateRasterDef(rid)
      tr.Commit()
    End Using
  End Sub

This proc is what I have sofar. The images are placed in the correct coordinate, but both with and height are set to 1. The real size of the image is 5110x3486, that should be scaled to 365x250 units. The debugged value of (Scale /rid.Size.X) is 0.0714 (that is correct). So the image scale should be 0.07, but after placing the images have a scale of 0.00055.

If I compare this to AutoCAD, there I have a function where I use twice a TransformBy method, one for Scaling and one for Displacement. If I use that in BricsCAD, only the Displacement is applied, the Scaling result in a width and height of zero.
The conclusion is justified that the initialization of the development of critical subsystem optimizes the probability of success to the development of the technical behavior over a given period.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8659
  • AKA Daniel
Re: Attaching Images in Bricscad 12 via .NET Code
« Reply #13 on: December 01, 2012, 02:59:27 AM »
Well, VB.Net reads good too :-)

You know I was just joking with you right? hmm maybe the veebee joke are getting long in the tooth  :|

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Attaching Images in Bricscad 12 via .NET Code
« Reply #14 on: December 01, 2012, 03:54:39 AM »
< .. >

You know I was just joking with you right? hmm maybe the veebee joke are getting long in the tooth  :|

You'll have a reason to dust off the veebeea joke book again next year Dan.  :|
nb: Please note that I have nothing against vb users ...

Added: how's the Bricscad .net implementation progressing ??

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.