Global Mapper Pro

wrapping gm structures in C#

mesgar
mesgar Global Mapper UserTrusted User
edited November 2011 in SDK
Hello

I am to use GM_LineFeature_t structure in C#. I wrapped necessary structures related the that one. but the problem arises when I want to set one of its fields (mPointList of type GM_Point_t). Is there any idea to solve the problem, please?

Is there any C# sample using GlobalMappers function at all?

Thanks in advance,
Hamed.

Comments

  • global_mapper
    global_mapper Administrator
    edited September 2009
    Hamed,

    There is a small C# sample at http://globalmapper.com/developer/SDK_Sample_CSharp.zip that demonstrates the basics of how to call the SDK from C#.

    If you still have trouble and want to provide your GM_LineFeature_t structure declaration I can take a look for you.

    Thanks,

    Mike
    Global Mapper Support
    support@globalmapper.com
  • mesgar
    mesgar Global Mapper User Trusted User
    edited September 2009
    Thanks for your reply,

    I've checked that sample, but I still have trouble using that structure,
    Could you please help me write a wrapper for that class?

    Thanks in advance.
  • global_mapper
    global_mapper Administrator
    edited September 2009
    I will try and update the C# sample application today to include a sample of getting line feature information. I'll let you know when that is updated.

    Thanks,

    Mike
    Global Mapper Support
    support@globalmapper.com
  • global_mapper
    global_mapper Administrator
    edited September 2009
    I have attached an upgraded C# example which includes calling GM_GetLineFeature and extracting vertex coordinates from the returned line.

    Let me know if I can be of further assistance.

    Thanks,

    Mike
    Global Mapper Support
    support@globalmapper.com
  • mesgar
    mesgar Global Mapper User Trusted User
    edited September 2009
    Thanks for your example, that was really helpful and useful

    I tried to implement GM_AddLineToCustomVectorLayer according to the sample code,
    I used GM_LineFeature_t structure as it was in the sample.

    But I still have trouble filing mAttrList field, could you please help me with a C# example
    showing how to implement and call this function?

    thanks in advance,
    Hamed.
  • global_mapper
    global_mapper Administrator
    edited September 2009
    Hamed,

    I will create a small sample and post it here when ready.

    Thanks,

    Mike
    Global Mapper Support
    support@globalmapper.com
  • global_mapper
    global_mapper Administrator
    edited September 2009
    Hamed,

    I have attached an updated sample application that creates a new area feature with an attribute value. This should be pretty much identical to the code needed for a new line feature.

    Let me know if I can be of further assistance.

    Thanks,

    Mike
    Global Mapper Support
    support@globalmapper.com
  • mesgar
    mesgar Global Mapper User Trusted User
    edited September 2009
    I really appreciate your help,

    I tried to use the code, but unfortunately It didn't work either.
    This time I'm putting the code I wrote.
    unsafe public void CreateVectorLayer(string path, string description, Point[] Points)
            {
                string theAttrName = "ELEVATION";
                string theAttrVal = "20 m";
    
                fixed (char* theAttrNamePtr = theAttrName, theAttrValPtr = theAttrVal)
                {
                    GM_AttrValue_t[] theAttrValList = new GM_AttrValue_t[1];
                    theAttrValList[0].mName = theAttrNamePtr;
                    theAttrValList[0].mVal = theAttrValPtr;
    
                    fixed (GM_AttrValue_t* theAttrValListPtr = &theAttrValList[0])
                    {
                        GM_Point_t[] tempmPointList = new GM_Point_t[Points.Length];
                        float[] elv = new float[Points.Length];
                        for (int i = 0; i < Points.Length; i++)
                        {
                            tempmPointList[i].mX = Points[i].X;
                            tempmPointList[i].mY = Points[i].Y;
                            elv[i] = (float)Points[i].Z;
                        }
                        fixed (GM_Point_t* thePointList = &tempmPointList[0])
                        {
                            fixed (float* theelvList = &elv[0])
                            {
    
                                Projection_t aProj = new Projection_t();
    
                                EarthDLL.GetProjection(out aProj);
                                IntPtr newlayer = EarthDLL.CreateCustomVectorLayer(description, ref aProj);
    
                                GM_LineFeature_t lineF = new GM_LineFeature_t();
                                lineF.mLineStyle.mFont.mFaceName = "Arial";
                                lineF.mFeatureInfo = new GM_VectorFeature_t();
    
                                lineF.mFeatureInfo.mNumAttrs = 1;
                                lineF.mFeatureInfo.mName = "a";
                                lineF.mFeatureInfo.mDesc = "a";
                                lineF.mFeatureInfo.mClass = 0;
                                lineF.mLineStyle = new GM_LineStyle_t();
                                lineF.mLineStyle.mDrawLabel = 0;
                                lineF.mLineStyle.mDrawLabelAlways = 0;
                                lineF.mLineStyle.mDrawLabelOnLine = 1;
                                lineF.mLineStyle.mLabelSegment = 1;
                                lineF.mLineStyle.mPenColor = 1;
                                lineF.mLineStyle.mPenWidth = 2;
                                lineF.mLineStyle.mReserved = 0;
                                lineF.mLineStyle.mFont = new GM_FontDef_t();
                                lineF.mLineStyle.mFont.mColor = 1;
                                lineF.mLineStyle.mFont.mFaceName = "Arial";
                                lineF.mLineStyle.mFont.mFixedHgt = 0.0F;
                                lineF.mLineStyle.mFont.mItalicize = 0;
                                lineF.mLineStyle.mFont.mPointSize = 2;
                                lineF.mLineStyle.mFont.mStrikeout = 0;
                                lineF.mLineStyle.mFont.mUnderline = 0;
    
                                lineF.mNumPoints = (uint)Points.Length;
    
                                lineF.mFeatureInfo.mAttrList = (IntPtr)theAttrValListPtr;
                                lineF.mPointList = (IntPtr)thePointList;
                                lineF.mVertexElevList = (IntPtr)theelvList;
    
                                EarthWrapper.EarthDLL.Error_t32 t = EarthDLL.AddLineToCustomVectorLayer(newlayer, ref lineF, true);
                                MessageBox.Show(t.ToString());
    
    
                                GM_ExportOptsSHP_t exportshp = new GM_ExportOptsSHP_t();
                                exportshp.mAddLabelAttr = 0;
                                exportshp.mAddLayerAttr = 0;
                                Rectangle_t rect = new Rectangle_t();
    
                                EarthDLL.ExportVector(path, GM_VectorExportFormat_t32.GM_Export_Shapefile, newlayer, ref rect, GM_VectorExportFlags_t32.GM_VectorExportFlags_ExportLines, exportshp);
                                EarthDLL.CloseLayer(newlayer);
                            }
                        }
                    }
                }
            }
    

    Running this code will come to this error:

    Attempted to read or write protected memory. This is often an indication that other memory is corrupt

    Could you please help me get rid of this error, too?
    Thanks in advance.
  • global_mapper
    global_mapper Administrator
    edited September 2009
    I think your problems are after your creation of the GM_LineFeature_t variable you are assigning members like mVectorFeature to a new GM_VectorFeature_t instance. You don't need to do that, the GM_LineFeature_t instantiation will have created those already.

    Other than that it looks like your export code is not correct. It looks like you are passing in a rectangle value of all zeroes for the bounds and also passing the Shapefile export options by value rather than by reference. What does your export vector function declaration look like?

    Thanks,

    Mike
    Global Mapper Support
    support@globalmapper.com
  • Osei
    Osei Global Mapper User Trusted User
    edited October 2009
    Your problem is quite easy and simple to solve with Marshalling. I have done something similar for the project I'm working on and will put out some source code for you to peruse. In the meantime, the C# sample app that comes with GM SDK will be your best resource. Not such a good idea using UNSAFE code unless it is for performance reasons. I haven't so far been successful with unsafe code.
  • PedroCunha
    PedroCunha Global Mapper User
    edited November 2011
    For those who want to know more about structures, this is a good, free video:
    Structures - Course C# 4.0 – Lesson 8

    Regards,

    Pedro Cunha
    http://mrbool.com
Sign In or Register to comment.