70001303 How can I apply a mapped image in a specific face of a vdPolyface

Article 70001303
Type HowTo
Product Engine
Version 7
Date Added 3/21/2018
Fixed 7.7015.0.1 (3/21/2018)
Submitted by Hien Ho


How can I apply a mapped image in a specific face of a vdPolyface? I get some strange results if the face of the vdPolyface is not in the X/Y plane.


For this code we will use this image:

See the code and comments below:

            vdDocument doc = framed.BaseControl.ActiveDocument;

            doc.New(); // create a new dowcument and add a "box" polyface
            bool res = doc.CommandAction.CmdBox3d(new gPoint(10, 10, 3), (double)3.0d, (double)3.0d, (double)3.0d, 

            if (!res) return;
            vdPolyface pface = doc.ActiveLayOut.Entities.Last as vdPolyface; // this the polyface just created by CmdBox3d
            if (pface == null) return;

            doc.CommandAction.CmdRotate3d("Y", pface, new gPoint(), (double)62.1d); // rotate it a bit 
            doc.CommandAction.View3D("VINW"); doc.CommandAction.View3D("SHADEON"); // set the view

            vdImageDef imgdef = doc.Images.Add(@"converter_200x200.png"); //add the image definition to be applied
            if (imgdef == null) return; 

            int choosenface = 3; // 0,1,2,..,7   8 faces in total In this face we will apply the material

            // get the position of the points that make the specific face
            int[] face = new int[] { pface.FaceList[choosenface * 5 ], pface.FaceList[choosenface * 5 + 1], pface.FaceList[choosenface * 5 + 3] };

            gPoint origin = new gPoint(pface.VertexList[face[0]]); // set which point will be the origin of the image
            gPoint xDir = new gPoint(pface.VertexList[face[1]]); // set which point will be the x direction of the image
            gPoint yDir = new gPoint(pface.VertexList[face[2]]); // set which point will be the y direction of the image

            Matrix mat = new Matrix(); // need to create the Matrix to be applied
            Vector xver = new Vector(origin, xDir); // based on the origin
            Vector yver = new Vector(origin, yDir); //xdir and ydir
            mat.SetFrom(origin, xver, yver);

            gPoint originTrans = mat.Transform(origin); // get the transformed origin
            gPoint xDirTrans = mat.Transform(xDir);// get the transformed x direction
            //create the mapped image
            double border = originTrans.Distance3D(xDirTrans) / 100.0d; // give a bit of a border arround
            vdMappedImage mappedimg = new vdMappedImage(imgdef, new gPoint(originTrans.x + border, originTrans.y + border, originTrans.z), 
                        originTrans.GetAngle(xDirTrans), originTrans.Distance3D(xDirTrans) - border, vdRectangle.Empty);

            // and transform it to match the face
            //and add it to the polyface
            //update and invalidate the polyface
            pface.Update(); pface.Invalidate();

The result:

Note that the image is applied to the whole polyface and not in a specific face or faces, so if there are faces in the projection of the image (like the bottom of this cubic polyface) then the image will be applied to them too.

Send comments on this topic.