TheSwamp

Code Red => .NET => Topic started by: twdotson on March 22, 2012, 08:47:58 PM

Title: Attaching Images in Bricscad 12 via .NET Code
Post by: twdotson 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).
Title: Re: Attaching Images in Bricscad 12 via .NET Code
Post by: It's Alive! on March 22, 2012, 09:43:59 PM
can you show your code?
Title: Re: Attaching Images in Bricscad 12 via .NET Code
Post by: twdotson 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 ?
Title: Re: Attaching Images in Bricscad 12 via .NET Code
Post by: It's Alive! on March 23, 2012, 10:48:31 PM
try moving the load method after ImageDic.SetAt.
Title: Re: Attaching Images in Bricscad 12 via .NET Code
Post by: twdotson on March 24, 2012, 04:38:30 PM
That made the difference, thanks.
Title: Re: Attaching Images in Bricscad 12 via .NET Code
Post by: huiz 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?


Title: Re: Attaching Images in Bricscad 12 via .NET Code
Post by: It's Alive! 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
Title: Re: Attaching Images in Bricscad 12 via .NET Code
Post by: huiz 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.
Title: Re: Attaching Images in Bricscad 12 via .NET Code
Post by: It's Alive! 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();
      }
    }
Title: Re: Attaching Images in Bricscad 12 via .NET Code
Post by: huiz 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 :-)
Title: Re: Attaching Images in Bricscad 12 via .NET Code
Post by: Kerry 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 ??
Title: Re: Attaching Images in Bricscad 12 via .NET Code
Post by: It's Alive! 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();
      }
    }
Title: Re: Attaching Images in Bricscad 12 via .NET Code
Post by: huiz 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.
Title: Re: Attaching Images in Bricscad 12 via .NET Code
Post by: It's Alive! 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  :|
Title: Re: Attaching Images in Bricscad 12 via .NET Code
Post by: Kerry 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 ??

Title: Re: Attaching Images in Bricscad 12 via .NET Code
Post by: huiz on December 01, 2012, 04:35:28 AM
..

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


Yes I know :-)

I also can read C# but I've chosen for VB a while ago. Maybe because I wrote a lot of VBA apps in the past, it seemed a natural choice. Also I write PHP scripts which is a C-language, and if I do some C# stuff, it is different from the PHP structure. I would have to correct every line of code then afterwards. With VB the structure is different so I don't have that problem.

But you may make jokes, I don't care :-)
Title: Re: Attaching Images in Bricscad 12 via .NET Code
Post by: huiz on December 01, 2012, 04:40:24 AM
Added: how's the Bricscad .net implementation progressing ??

It is almost the level of AutoCAD 2010 API, there is not much that doesn't work in BricsCAD. Even Palettes are supported now (except the Palette Icon).

Further there are very little differences, like the impossibility of returning an objectid from an object after committing the transaction. You need to create an extra object for a return value.

I'm very positive about the Bricscad API.