| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287 | using MyControls;using OTS.WinFormsUI.Docking;using OTSDataType;using OTSMeasureApp._0_OTSModel.OTSDataType;using OTSMeasureApp._4_OTSSamplespaceGraphicsPanel;using OTSMeasureApp._4_OTSSamplespaceGraphicsPanel.VisualGDIObjects;using OTSModelSharp;using OTSModelSharp.ServiceCenter;using System;using System.Collections;using System.Collections.Generic;using System.Drawing;using System.Windows.Forms;namespace OTSMeasureApp{    public enum EnumMousePointPosition    {        MouseSizeNone = 0, //'无          MouseSizeRight = 1, //'拉伸右边框          MouseSizeLeft = 2, //'拉伸左边框          MouseSizeBottom = 3, //'拉伸下边框          MouseSizeTop = 4, //'拉伸上边框          MouseSizeTopLeft = 5, //'拉伸左上角          MouseSizeTopRight = 6, //'拉伸右上角          MouseSizeBottomLeft = 7, //'拉伸左下角          MouseSizeBottomRight = 8, //'拉伸右下角          MouseDrag = 9   // '鼠标拖动      }    public enum ContextMenuType    {        //样品孔右键菜单        SampleHoleMenu = 0,        //样品右键菜单        SampleMenu = 1,        //测量区域右键菜单        MeasureMenu = 2,        //线程运行后菜单        ThreadRunMenu = 3,        //样品台右键菜单        SampleStateMenu = 4,        //样品孔BSE图像        SampleHoleBSEImage = 5,        //帧图菜单        FieldMenu = 6    }    public enum MenuIndex    {        AddSample = 0,        DeleteSample = 1,        SampleStripSeparator = 2,        ReadSEMData = 3,        SetSemData = 4,        SetSEMCenterLocation = 5,        DriveSEMToCenterLocation = 6,        DriveSEMToCurrentLocation = 7,        SEMStripSeparator = 8,        ShootBSEPicture = 9,        DeleteBSEPicture = 10,        DeleteSampleData = 11,        SlopFocus = 12    }    public enum DrawMeasureAreaActionType    {        NoAction = 1,        //圆形        Circle = 2,        Rectangle = 3,        //多边形        Polygon = 4,        //圆形三点法        CircleByThreePoints = 5,        InverseSelection = 6    }    public partial class OTSSamplespaceWindow : DockContent    {        #region 全局变量定义        public CVisualStage m_visualStage;        string m_SampleHoleSelectName = string.Empty;        public string m_SampleSelectName = string.Empty;        public List<CVisualSampleArea> m_visualSamples;        //手绘测量区域        public static CMeasureArea m_DrawMeasureGDIObject = new CMeasureArea();        //多边形测量区域完成标识        public static CDisplayGDIObject m_DrawPolygonFinishGDIObject;        //主窗体对象        OTSIncAMeasureAppForm m_MeasureAppForm;        SEMDATAFieldManage m_SEMDATAFieldManage;        private _5_OTSMeasureStatuImageFun.SlopFocus slopFocus = null;        const float m_PenWidth = 5;        const int m_MinMeasureWidthValue = 20;        //点数量        const int m_PolygonMinPointCount = 2;        //与多边形绘制的开始点相差的像素值        const int m_OffsetPX = 10;        //默认值        const int m_DValue = 1;        #region 手动绘制测量区域        Point m_MouseDownPoint;        Point m_MouseMovePoint;        //手动绘制多边形测量区域完成标识        bool m_IsDrawPolygonFinish = false;        //手动绘制测量区域的形状类型        DrawMeasureAreaActionType m_DrawMeasureType = DrawMeasureAreaActionType.NoAction;        //是否手绘标识        bool m_IsDrawMeasure = false;        #endregion        #endregion        NLog.Logger log;        #region 标尺变量        //最小缩放倍数        const float f_zoom_mix = 0.5f;        //标尺的大小记录        private float f_ruler_size = 100;        //标尺原先大小的记录,参与计算        private float f_old_ruler_size = 100;        #endregion        //国际化        OTSCommon.Language lan;        Hashtable table;      public CDisplayGDIObject sampleHoleImageBuffer = new CDisplayGDIObject(new RectangleF(0, 0, 10, 10), GDIType.SampleHoleImageBuffer);        #region 设置双缓冲        /// <summary>        /// 设置双缓冲        /// </summary>        public void SetDoubleBufferByIsDraw()        {            SetStyle(ControlStyles.UserPaint, true);            SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true); // 双缓冲        }        #endregion        CFieldPositionHelper pFieldMgr = new CFieldPositionHelper();        #region 构造函数 获取父窗体对象        public OTSSamplespaceWindow(OTSIncAMeasureAppForm MeasureAppForm)        {            InitializeComponent();            m_MeasureAppForm = MeasureAppForm;            SetDoubleBufferByIsDraw();// 双缓冲            //国际化            lan = new OTSCommon.Language(this);            table = lan.GetNameTable(this.Name);        }        #region 菜单操作        private void CMStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)        {            Point mousePoint = this.PointToClient(Control.MousePosition);            CMStrip.Hide();            switch (e.ClickedItem.Name)            {                case "AddStage":                    //添加样品                    m_MeasureAppForm.AddNewSampleMeasure(m_SampleHoleSelectName);                    break;                case "DeleteStage":                    //删除样品                    DeleteSampleInfo(m_SampleSelectName);                    break;                case "DriveSEMToCenterLocation":                    //驱动SEM到中心位置                    SetSEMToCenterPoint();                    ReadSEMDataAndSetToCurrentSample();                    //PrepareMeasureField(GetWorkingVisualSample(), MSR_RUN_TYPE.RUNMEASURE);                    break;                case "DriveSEMToCurrentLocation":                    //驱动SEM到当前位置                    SetSEMCurrentLocation();                    ReadSEMDataAndSetToCurrentSample();                    //PrepareMeasureField(GetWorkingVisualSample(), MSR_RUN_TYPE.RUNMEASURE);                    break;                case "SetSEMCenterLocation":                    //将测量区域移到SEM当前位置                    SetMeasureToSEMLocation();                    ReadSEMDataAndSetToCurrentSample();                    PrepareMeasureField(GetWorkingVisualSample(), MSR_RUN_TYPE.RUNMEASURE);                    break;                case "ReadSEMData":                    m_MeasureAppForm.SetRunType(MSR_RUN_TYPE.RUNMEASURE);                    ReadSEMDataAndSetToCurrentSample();                    PrepareMeasureField(GetWorkingVisualSample(), MSR_RUN_TYPE.RUNMEASURE);                    break;                case "SetSemData":                    double SemMag = 0;                    double dDistance = 0;                    double bri = 0;                    double contra = 0;                    double kv = 0;                    if (m_MeasureAppForm.m_ProjParam.GetWorkingSampleSEMData(ref dDistance, ref SemMag, ref bri, ref contra, ref kv))                    {                        //设置SEM数据                        if (!SetSEMDATAMParameter(SemMag, dDistance, bri, contra))                        {                            log.Error("Failed SetSemData:--Mag:" + SemMag + "--Distance:" + dDistance + "--");                        }                    }                    break;                case "ShootBSEPicture":                    //设置消息类型 拍摄样品孔                    m_MeasureAppForm.SetRunType(MSR_RUN_TYPE.RUNSampleHoleImage);                    PrepareMeasureField(GetWorkingVisualSample(), MSR_RUN_TYPE.RUNSampleHoleImage);                    m_MeasureAppForm.StartSampleHoleBSEImage();                    break;                case "DeleteBSEPicture":                    DeleteHoleBSEImageDataNoMessageBox();                    break;                case "DeleteSampleData":                    try                    {                        var frmFieldStart = new frmInitializeStartInput();                        frmFieldStart.ShowDialog();                        if (frmFieldStart.okcancel == true)                        {                            InitSampleMeasure(m_MeasureAppForm.m_ProjParam.GetWorkSample(), frmFieldStart.fieldStartNo);                        }                    }                    catch (Exception ex)                    {                        MessageBox.Show(ex.ToString());                    }                    break;                case "SlopFocusMenuItem":                    if (slopFocus == null)                    {                        slopFocus = new _5_OTSMeasureStatuImageFun.SlopFocus(this);                    }                    slopFocus.Show();                    break;            }        }        #endregion        public void InitSampleMeasure(COTSSample smpl, int fieldStartNo)        {            m_MeasureAppForm.m_ProjParam.SetWorkSample(smpl.GetName());            m_MeasureAppForm.m_ProjParam.InitWorkingSampleData(fieldStartNo);            m_MeasureAppForm.InitCurrentSampleState();            m_MeasureAppForm.m_MsrThreadWrapper.m_measure.mapSmplMsr.Clear();            var visualsam = GetWorkingVisualSample();            var sam = m_MeasureAppForm.m_ProjRstData.GetWorkingSample();            var allflds = sam.GetFieldsData();            foreach (var f in allflds)            {                if (f.GetMeasureSequence() >= fieldStartNo)                {                    Color ColorStr = OTSSamplespaceGraphicsPanelFun.GetColorValue(ColorType.FieldColor);                    ChangeMeasureFieldColor(visualsam, f.GetOTSPosition(), ColorStr);                }            }            m_MeasureAppForm.m_MeasureResultWindow.UpdateResultInfoBySampleName(sam.GetName());        }        /// <summary>        /// 画圆形测试区域 事件        /// </summary>        public void DrawCircleMeasure()        {            //手绘测量区域类型为圆形            m_DrawMeasureType = DrawMeasureAreaActionType.Circle;            m_IsDrawMeasure = true;            m_DrawMeasureGDIObject = null;            this.Cursor = Cursors.Hand;        }        /// <summary>        /// 画矩形测试区域 事件        /// </summary>        public void DrawRectangleMeasure()        {            //手绘测量区域类型为矩形            m_DrawMeasureType = DrawMeasureAreaActionType.Rectangle;            m_IsDrawMeasure = true;            m_DrawMeasureGDIObject = null;            this.Cursor = Cursors.Hand;        }        /// <summary>        /// 画多边形测试区域 事件        /// </summary>        public void DrawPolygonMeasure()        {            m_DrawMeasureType = DrawMeasureAreaActionType.Polygon;            m_IsDrawMeasure = true;            //m_DrawMeasureGDIObject = null;            m_DrawMeasureGDIObject = new CMeasureArea(new List<PointF>(), ShapeType.POLYGON, "", "", Color.Red);            this.Cursor = Cursors.Hand;        }        /// <summary>        /// 三点画圆形测试区域 事件  先多边形后圆形        /// </summary>        public void DrawCircleMeasureByThreePoints()        {            //手绘测量区域类型为圆形            m_DrawMeasureType = DrawMeasureAreaActionType.CircleByThreePoints;            m_IsDrawMeasure = true;            m_DrawMeasureGDIObject = null;            this.Cursor = Cursors.Hand;        }        public void DrawRingShapeArea()        {            //手绘测量区域类型为圆形            RingGdiDataInput frmInput = new RingGdiDataInput();            if (frmInput.ShowDialog() == DialogResult.OK)            {                float diameter = (float)m_visualStage.MicronConvertToPixel(frmInput.Diameter);                float ringwidth = (float)m_visualStage.MicronConvertToPixel(frmInput.RingWidth);                var sam = GetWorkingVisualSample();                SizeF size = new SizeF(diameter, diameter);                var position = sam.GetMeasureGDIObject().GetZoomedRegionF().Location;                RectangleF rec = sam.GetMeasureGDIObject().GetZoomedRegionF();                PointF centerPos = new PointF(position.X + rec.Width / 2, position.Y + rec.Height / 2);                PointF newRegionFPos = new PointF(centerPos.X - diameter / 2, centerPos.Y - diameter / 2);                RectangleF rectangle = new RectangleF(newRegionFPos, size);                Color MeasureColor = Color.Red;                CRingGDIObject newGDI;                newGDI = new CRingGDIObject(rectangle, sam.GetSampleGDIObject().HoleNo, sam.GetSampleName(), MeasureColor);                newGDI.SetRingWidth(ringwidth);                newGDI.SetZoomNumber(m_visualStage.GetZoomNum());                newGDI.SetDisplayRefPoint(m_visualStage.GetDisplayRefPoint());                newGDI.IsWorkSample = true;                sam.SetMeasureGDIObject(newGDI);                m_DrawPolygonFinishGDIObject = null;                m_DrawMeasureGDIObject = null;                var para = m_visualStage.GetSampleMeasurePara(sam.GetMeasureGDIObject());                m_MeasureAppForm.m_ProjParam.SetWorkSampleHoleAndMeasureArea(para);            }        }        public CVisualSampleArea GetWorkingVisualSample()        {            foreach (var s in m_visualSamples)            {                if (s.IsWorkSample())                {                    return s;                }            }            return null;        }        public CVisualSampleArea GetVisualSampleByName(string name)        {            foreach (var s in m_visualSamples)            {                if (s.GetSampleName() == name)                {                    return s;                }            }            return null;        }        public void DisplayVisualStage(CStage SStage, CSEMStageData SEMStageData)        {            m_visualStage.clear();            m_visualStage.InitSampleStageData(SStage, SEMStageData, this.Width, this.Height);            m_visualStage.DrawSampleStage();        }        public void DisplayExistSamples(List<SampleMeasurePara> SMeasrueParaList)        {            foreach (var SMeasrueArea in SMeasrueParaList)            {                DisplaySample(SMeasrueArea);            }            SwitchWorkSample(SMeasrueParaList[0].sSampleName);            m_SampleSelectName = SMeasrueParaList[0].sSampleName;            //重新绘制            Invalidate();        }                public void DisplaySample(SampleMeasurePara SMeasrueArea)        {            var m_Sample = m_MeasureAppForm.m_ProjParam.GetResultData().GetSampleByName(SMeasrueArea.sSampleName);            var FldDatas = m_Sample.GetFieldsData();            if (FldDatas.Count == 0)            {                //this part code is to be compatible with previouse version result file which there's no AllField position info.                DrawSampleAndMeasureInfo(SMeasrueArea);            }            else            {                var item = m_visualStage.GetHoleGDIByHoleName(SMeasrueArea.sampleHoleName);                if (item != null)                {                    CVisualSampleArea sam;                    if (InitVisualSample(SMeasrueArea.sampleHoleName, SMeasrueArea.sSampleName, out sam))                    {                        if (SMeasrueArea.iShape == ShapeType.POLYGON)                        {                            var ps = new List<PointF>();                            foreach (var p in SMeasrueArea.DrawPolygonPointList)                            {                                var ctrlcoordPoint = m_visualStage.OTSCoordToCtrlCoord(p);                                ps.Add(ctrlcoordPoint);                            }                            CMeasureArea polygonGdi = new CMeasureArea(ps, ShapeType.POLYGON, SMeasrueArea.sampleHoleName, SMeasrueArea.sSampleName, Color.Red);                            sam.SetMeasureGDIObject(polygonGdi);                        }                        else                        {                            PointF xHole = new PointF(SMeasrueArea.MeasureRect.Left, SMeasrueArea.MeasureRect.Top);                            PointF yHole = new PointF(SMeasrueArea.MeasureRect.Right, SMeasrueArea.MeasureRect.Bottom);                            CDisplayGDIObject gdi = m_visualStage.GetCtrlCoordGDI(xHole, yHole, m_visualStage. ConvertShapeTypeToGDIType(SMeasrueArea.iShape), "", "");                            gdi.SampleName = SMeasrueArea.sSampleName;                            gdi.HoleNo = SMeasrueArea.sampleHoleName;                            CMeasureArea measureGdi;                            if (m_visualStage.GetMeasureGdiObjectFromSampleGdi(gdi, out measureGdi))                            {                                sam.SetMeasureGDIObject(measureGdi);                            }                        }                        List<CVisualFieldGDIObject> visualfieldRects = new List<CVisualFieldGDIObject>();                        foreach (var fld in FldDatas)                        {                            float micronWidth = (float)fld.GetOTSRect().GetWidth();                            float micronHeight = (float)fld.GetOTSRect().GetHeight();                            var fldRect = new RectangleF(new PointF(fld.GetOTSPosition().X - micronWidth / 2, fld.GetOTSPosition().Y - micronHeight / 2), new SizeF(micronWidth, micronHeight));                            PointF lt = new PointF(fldRect.Left, fldRect.Top);                            PointF rb = new PointF(fldRect.Right, fldRect.Bottom);                            RectangleF visualRect = m_visualStage.GetCtrlCoordRectF(lt, rb);                            var ColorStr = OTSSamplespaceGraphicsPanelFun.GetColorValue(ColorType.FieldColor);                            string sampleName = sam.GetSampleName();                            string sampleHoleName = sam.GetSampleGDIObject().HoleNo;                            CVisualFieldGDIObject createRect = new CVisualFieldGDIObject(visualRect, (int)fld.GetOTSPosition().X, (int)fld.GetOTSPosition().Y, sampleHoleName, sampleName, ColorStr);                            createRect.Enable = fld.Enable;                            visualfieldRects.Add(createRect);                        }                        sam.GetMeasureGDIObject().SubItems().Clear();                        foreach (var r in visualfieldRects)                        {                            sam.AddFieldGDIObject(r);                        }                        COTSMsrPrjResultData cProjMgr = m_MeasureAppForm.m_ProjParam.GetResultData();                        List<PointF> CompleteFieldList = cProjMgr.GetSampleByName(SMeasrueArea.sSampleName).GetMsrStatus().GetCompletedFieldsCenter();                        if (CompleteFieldList.Count > 0)                        {                            ChangeCompleteFieldMeasureState(CompleteFieldList, Color.Green, sam);                        }                    }                }            }        }        public void SwitchWorkSample(string sNewWSampleName, bool bDrawField = false)        {            try            {                for (int i = 0; i < m_visualSamples.Count; i++)                {                    //获取颜色                    if (m_visualSamples[i].GetSampleName() == sNewWSampleName)                    {                        var theColor = OTSSamplespaceGraphicsPanelFun.GetColorValue(ColorType.SampleSelColor);                        m_visualSamples[i].GetSampleGDIObject().SelColor = theColor;                        m_visualSamples[i].SetIsWorkSample(true);                    }                    else                    {                        var SampleColor = OTSSamplespaceGraphicsPanelFun.GetColorValue(ColorType.SampleColor);                        m_visualSamples[i].GetSampleGDIObject().SelColor = SampleColor;                        m_visualSamples[i].SetIsWorkSample(false);                    }                }                m_IsDrawMeasure = false;                this.Invalidate();                return;            }            catch (Exception ex)            {                log.Trace(ex.ToString());            }        }        public void ChangeCompleteFieldMeasureState(List<PointF> CompleteFieldList, Color changeColor, CVisualSampleArea vsSam)        {            foreach (var p in CompleteFieldList)            {                ChangeMeasureFieldColor(vsSam, p, changeColor);            }        }        public void ChangeMeasureFieldColor(CVisualSampleArea vsSam, PointF CurrentFIELDOTSPoint, Color changeColor)        {            foreach (var gdi in vsSam.GetMeasureFieldGDIObjects())            {                var fieldPoint = m_visualStage.OTSCoordToCtrlCoord(new PointF((float)CurrentFIELDOTSPoint.X, (float)CurrentFIELDOTSPoint.Y));                if (gdi.IfContains(fieldPoint))                {                    //修改完成状态                    gdi.SelColor = changeColor;                    break;                }            }        }        public void ChangeWorkSampleName(string sNewWSampleName)        {            //更换样品名称            var sam = GetWorkingVisualSample();            sam.SetSampleName(sNewWSampleName);            this.Invalidate();            return;        }        public void SetMeasureState(Color setColor)        {            foreach (var items in m_visualSamples)            {                items.GetMeasureGDIObject().SelColor = setColor;            }        }        #endregion        public void SetSampleState(Color setColor)        {            foreach (var items in m_visualSamples)            {                if (items.GetSampleGDIObject().IsDragging)                {                    //设置颜色                    var ColorStr = OTSSamplespaceGraphicsPanelFun.GetColorValue(ColorType.SampleSelColor);                    items.GetSampleGDIObject().SelColor = ColorStr;                }                else                {                    items.GetSampleGDIObject().SelColor = setColor;                }            }        }        public bool DeleteSample(string SampleName)        {            var sam = GetVisualSampleByName(SampleName);            m_visualStage.DecreaseSampleCount(SampleName);            m_visualSamples.Remove(sam);            var ColorStr = OTSSamplespaceGraphicsPanelFun.GetColorValue(ColorType.SampleColor);            SetSampleState(ColorStr);            SetMeasureState(ColorStr);            return false;        }        public void DeleteWorkSample(string sDeleteWSampleName)        {            try            {                DeleteSample(sDeleteWSampleName);            }            catch (Exception ex)            {                log.Error(ex.ToString());            }        }        #region 更改工作样品中 测量区域形状        public void changeShape(ShapeType iShape)        {            try            {                string sampleName = m_MeasureAppForm.m_ProjParam.GetWorkSampleName();                //m_Shap 更换样品形状                var sam = GetWorkingVisualSample();                var itemMeasure = sam.GetMeasureGDIObject();                itemMeasure.Myshape = iShape;                var sampleMeasurePara = m_visualStage.GetSampleMeasurePara(sam.GetMeasureGDIObject());                m_MeasureAppForm.m_ProjParam.SetWorkSampleHoleAndMeasureArea(sampleMeasurePara);                PrepareMeasureField(sam, m_MeasureAppForm.GetRunType());                this.Invalidate();                return;            }            catch (Exception ex)            {                log.Error(ex.ToString());            }        }        #endregion        public void AddSampleArea(SampleMeasurePara SMeasurePara)        {            try            {                CMStrip.Hide();                CVisualSampleArea sam;                //添加样品                foreach (var itemSample in m_visualSamples)                {                    //设置颜色                    var ColorStr = OTSSamplespaceGraphicsPanelFun.GetColorValue(ColorType.SampleColor);                    itemSample.GetSampleGDIObject().SelColor = ColorStr;                    itemSample.SetIsWorkSample(false);                }                if (m_visualStage.GetVisualSampleArea(SMeasurePara, out sam))                {                    sam.SetIsWorkSample(true);                    m_visualSamples.Add(sam);                    m_SampleSelectName = SMeasurePara.sSampleName;                    var para = m_visualStage.GetSampleMeasurePara(sam.GetMeasureGDIObject());                    //var para = SMeasurePara;                    m_MeasureAppForm.m_ProjParam.SetWorkSampleHoleAndMeasureArea(para);                    Invalidate();                }            }            catch (Exception ex)            {                log.Error(ex.ToString());            }        }        public bool InitVisualSample(string SampleHoleNo, string SampleName, out CVisualSampleArea outsample)        {            CDisplayGDIObject itemAdd;            //设置样品选择状态为非工作样品            foreach (var Sample in m_visualSamples)            {                var itemSample = Sample.GetSampleGDIObject();                //设置颜色                var ColorStr = OTSSamplespaceGraphicsPanelFun.GetColorValue(ColorType.SampleColor);                itemSample.SelColor = ColorStr;                itemSample.IsWorkSample = false;            }            itemAdd = m_visualStage.GetHoleGDIByHoleName(SampleHoleNo);            if (itemAdd != null)            {                //获取样品台类型                //var shape = itemAdd.Shape;                //设置颜色                var ColorStr = OTSSamplespaceGraphicsPanelFun.GetColorValue(ColorType.SampleSelColor);                Color selColor = ColorStr;                CDisplayGDIObject sample = new CDisplayGDIObject(itemAdd.GetOrigionalDrawRegionF(), itemAdd.GDIType, itemAdd.HoleNo, selColor);                //累加样品数量                itemAdd.SampleCount += 1;                itemAdd.IsWorkSample = true;                sample.SampleName = SampleName;                sample.HoleNo = itemAdd.HoleNo;                var newSam = new CVisualSampleArea();                newSam.SetSampleGDIObject(sample);                newSam.SetSampleName(sample.SampleName);                m_visualSamples.Add(newSam);                outsample = newSam;                return true;            }            outsample = null;            return false;        }        #region 设置工作样品        /// <summary>        /// 设置工作样品与测量区域        /// </summary>        /// <param name="sampleName">样品名称</param>        public void SetSampleOrder(string sampleName)        {            //修改测量区域集合 索引     (添加样品或者切换样品时,将当前工作样品显示在样品孔的最上)            m_visualSamples = SelectMeasureIndexIsTop(m_visualSamples, sampleName);        }        #endregion        private List<CVisualSampleArea> SelectMeasureIndexIsTop(List<CVisualSampleArea> objList, string SampleName)        {            List<CVisualSampleArea> objListTemp = objList;            CVisualSampleArea itemTemp;            foreach (CVisualSampleArea item in objList)            {                if (item.GetSampleName() == SampleName)                {                    //临时记录所选测量区域                    itemTemp = item;                    //删除原信息                    objListTemp.Remove(item);                    //添加新信息                    objListTemp.Insert(objListTemp.Count, itemTemp);                    break;                }            }            return objListTemp;        }        //响应委托事件的样品台数据        public void InitStageDisplay(CStage SStage, CSEMStageData SEMStageData)        {            m_visualStage.InitSampleStageData(SStage, SEMStageData, this.Width, this.Height);            //清除所有绘制的图形信息            ClearDrawInfo();            //绘制样品台信息            m_visualStage.DrawSampleStage();            //标尺初始化            RulerInit(m_visualStage.GetOTSSampleStageData());            ShowRulerLocation();            if (m_visualSamples.Count == 0)            {                //将测量状态初始化                m_MeasureAppForm.m_MessureStatuWindow.SetMeasureStatusInit();            }            Invalidate();        }        private void GetTriangleExcenterRadius(PointF px1, PointF px2, PointF px3, out float R, out PointF center)        {            float x1, y1, x2, y2, x3, y3;            float a, b;            x1 = px1.X;            y1 = px1.Y;            x2 = px2.X;            y2 = px2.Y;            x3 = px3.X;            y3 = px3.Y;            a = ((y2 - y1) * (y3 * y3 - y1 * y1 + x3 * x3 - x1 * x1) - (y3 - y1) * (y2 * y2 - y1 * y1 + x2 * x2 - x1 * x1)) / (2 * ((x3 - x1) * (y2 - y1) - (x2 - x1) * (y3 - y1)));            b = ((x2 - x1) * (x3 * x3 - x1 * x1 + y3 * y3 - y1 * y1) - (x3 - x1) * (x2 * x2 - x1 * x1 + y2 * y2 - y1 * y1)) / (2 * ((y3 - y1) * (x2 - x1) - (y2 - y1) * (x3 - x1)));            center = new PointF(a, b);            R = (float)Math.Sqrt((x1 - a) * (x1 - a) + (y1 - b) * (y1 - b));        }        #region 窗体事件        private void OTSSamplespaceWindow_Load(object sender, EventArgs e)        {            m_visualStage = new CVisualStage();            m_visualSamples = new List<CVisualSampleArea>();            this.ResizeRedraw = false;            control_Ruler2 = new Control_Ruler();            Point rulerLocation = new Point(10, this.Height - (int)(control_Ruler2.Height * 2));            control_Ruler2.Location = rulerLocation;            this.Controls.Add(control_Ruler2);            log = NLog.LogManager.GetCurrentClassLogger();            pictureBox1.BackColor = Color.Transparent;            sampleHoleImageBuffer = new CDisplayGDIObject(new RectangleF(0, 0, m_MeasureAppForm.Width, m_MeasureAppForm.Height), GDIType.SampleHoleImageBuffer); ;            sampleHoleImageBuffer.BSEImage = new Bitmap(m_MeasureAppForm.Width, m_MeasureAppForm.Height);//the m_measureAppForm.Width and Height is large enough to contain the BSE image and not changed.        }        public static void SetShowCMStrip(ContextMenuType menuType, ContextMenuStrip cmStrip)        {            switch (menuType)            {                case ContextMenuType.SampleHoleMenu:                    cmStrip.Items[(int)MenuIndex.AddSample].Enabled = true;                    cmStrip.Items[(int)MenuIndex.DeleteSample].Enabled = false;                    cmStrip.Items[(int)MenuIndex.ReadSEMData].Enabled = false;                    cmStrip.Items[(int)MenuIndex.SetSemData].Enabled = false;                    cmStrip.Items[(int)MenuIndex.SetSEMCenterLocation].Enabled = false;                    cmStrip.Items[(int)MenuIndex.DriveSEMToCenterLocation].Enabled = false;                    cmStrip.Items[(int)MenuIndex.DriveSEMToCurrentLocation].Enabled = true;                    cmStrip.Items[(int)MenuIndex.SEMStripSeparator].Enabled = false;                    cmStrip.Items[(int)MenuIndex.ShootBSEPicture].Enabled = false;                    cmStrip.Items[(int)MenuIndex.DeleteBSEPicture].Enabled = false;                    cmStrip.Items[(int)MenuIndex.DeleteSampleData].Enabled = false;                    cmStrip.Items[(int)MenuIndex.SlopFocus].Enabled = false;                    break;                case ContextMenuType.SampleMenu:                    cmStrip.Items[(int)MenuIndex.AddSample].Enabled = true;                    cmStrip.Items[(int)MenuIndex.DeleteSample].Enabled = true;                    cmStrip.Items[(int)MenuIndex.ReadSEMData].Enabled = true;                    cmStrip.Items[(int)MenuIndex.SetSemData].Enabled = false;                    cmStrip.Items[(int)MenuIndex.SetSEMCenterLocation].Enabled = false;                    cmStrip.Items[(int)MenuIndex.DriveSEMToCenterLocation].Enabled = false;                    cmStrip.Items[(int)MenuIndex.DriveSEMToCurrentLocation].Enabled = true;                    cmStrip.Items[(int)MenuIndex.SEMStripSeparator].Enabled = false;                    cmStrip.Items[(int)MenuIndex.ShootBSEPicture].Enabled = true;                    cmStrip.Items[(int)MenuIndex.DeleteBSEPicture].Enabled = false;                    cmStrip.Items[(int)MenuIndex.DeleteSampleData].Enabled = true;                    cmStrip.Items[(int)MenuIndex.SlopFocus].Enabled = true;                    break;                case ContextMenuType.MeasureMenu:                    cmStrip.Items[(int)MenuIndex.AddSample].Enabled = true;                    cmStrip.Items[(int)MenuIndex.DeleteSample].Enabled = true;                    cmStrip.Items[(int)MenuIndex.ReadSEMData].Enabled = true;                    cmStrip.Items[(int)MenuIndex.SetSemData].Enabled = true;                    cmStrip.Items[(int)MenuIndex.SetSEMCenterLocation].Enabled = true;                    cmStrip.Items[(int)MenuIndex.DriveSEMToCenterLocation].Enabled = true;                    cmStrip.Items[(int)MenuIndex.DriveSEMToCurrentLocation].Enabled = true;                    cmStrip.Items[(int)MenuIndex.ShootBSEPicture].Enabled = true;                    cmStrip.Items[(int)MenuIndex.DeleteBSEPicture].Enabled = true;                    cmStrip.Items[(int)MenuIndex.DeleteSampleData].Enabled = false;                    cmStrip.Items[(int)MenuIndex.SlopFocus].Enabled = true;                    break;                case ContextMenuType.FieldMenu:                    cmStrip.Items[(int)MenuIndex.AddSample].Enabled = true;                    cmStrip.Items[(int)MenuIndex.DeleteSample].Enabled = true;                    cmStrip.Items[(int)MenuIndex.ReadSEMData].Enabled = true;                    cmStrip.Items[(int)MenuIndex.SetSemData].Enabled = true;                    cmStrip.Items[(int)MenuIndex.SetSEMCenterLocation].Enabled = true;                    cmStrip.Items[(int)MenuIndex.DriveSEMToCenterLocation].Enabled = true;                    cmStrip.Items[(int)MenuIndex.DriveSEMToCurrentLocation].Enabled = true;                    cmStrip.Items[(int)MenuIndex.ShootBSEPicture].Enabled = true;                    cmStrip.Items[(int)MenuIndex.DeleteBSEPicture].Enabled = true;                    cmStrip.Items[(int)MenuIndex.DeleteSampleData].Enabled = true;                    cmStrip.Items[(int)MenuIndex.SlopFocus].Enabled = true;                    break;                case ContextMenuType.ThreadRunMenu:                    for (int i = 0; i < cmStrip.Items.Count; i++)                    {                        cmStrip.Items[i].Enabled = false;                    }                    break;                case ContextMenuType.SampleStateMenu:                    cmStrip.Items[(int)MenuIndex.AddSample].Enabled = false;                    cmStrip.Items[(int)MenuIndex.DeleteSample].Enabled = false;                    cmStrip.Items[(int)MenuIndex.ReadSEMData].Enabled = false;                    cmStrip.Items[(int)MenuIndex.SetSemData].Enabled = false;                    cmStrip.Items[(int)MenuIndex.SetSEMCenterLocation].Enabled = false;                    cmStrip.Items[(int)MenuIndex.DriveSEMToCenterLocation].Enabled = false;                    cmStrip.Items[(int)MenuIndex.DriveSEMToCurrentLocation].Enabled = true;                    cmStrip.Items[(int)MenuIndex.ShootBSEPicture].Enabled = false;                    cmStrip.Items[(int)MenuIndex.DeleteBSEPicture].Enabled = false;                    cmStrip.Items[(int)MenuIndex.DeleteSampleData].Enabled = false;                    cmStrip.Items[(int)MenuIndex.SlopFocus].Enabled = false;                    break;                case ContextMenuType.SampleHoleBSEImage:                    cmStrip.Items[(int)MenuIndex.AddSample].Enabled = true;                    cmStrip.Items[(int)MenuIndex.DeleteSample].Enabled = false;                    cmStrip.Items[(int)MenuIndex.ReadSEMData].Enabled = false;                    cmStrip.Items[(int)MenuIndex.SetSemData].Enabled = false;                    cmStrip.Items[(int)MenuIndex.SetSEMCenterLocation].Enabled = false;                    cmStrip.Items[(int)MenuIndex.DriveSEMToCenterLocation].Enabled = false;                    cmStrip.Items[(int)MenuIndex.DriveSEMToCurrentLocation].Enabled = true;                    cmStrip.Items[(int)MenuIndex.ShootBSEPicture].Enabled = true;                    cmStrip.Items[(int)MenuIndex.DeleteBSEPicture].Enabled = true;                    cmStrip.Items[(int)MenuIndex.DeleteSampleData].Enabled = false;                    cmStrip.Items[(int)MenuIndex.SlopFocus].Enabled = false;                    break;            }        }        private List<CDisplayGDIObject> GetAllVisualSampleGDIObject()        {            List<CDisplayGDIObject> allobj = new List<CDisplayGDIObject>();            foreach (var s in m_visualSamples)            {                allobj.Add(s.GetSampleGDIObject());                allobj.Add(s.GetMeasureGDIObject());                           }            allobj.AddRange(m_visualStage.FrameOfHoleGDIObjects);            allobj.Add(sampleHoleImageBuffer);            return allobj;        }        private List<CDisplayGDIObject> GetOnlyVisualSampleGDIObject()        {            List<CDisplayGDIObject> allobj = new List<CDisplayGDIObject>();            foreach (var s in m_visualSamples)            {                allobj.Add(s.GetSampleGDIObject());            }            return allobj;        }        private List<CDisplayGDIObject> GetAllVisualFieldGdiObject()        {            List<CDisplayGDIObject> allobj = new List<CDisplayGDIObject>();            foreach (var s in m_visualSamples)            {                allobj.Add(s.GetMeasureGDIObject());                //var flds = s.GetMeasureFieldGDIObjects();                //for (int i = flds.Count; i > 0; i--)//revert the sequence so that the last field will paint first.then the green or yellow rectangle can display fully.                //{                //    var f = flds[i - 1];                //    allobj.Add(f);                //}            }            return allobj;        }        private EnumMousePointPosition MousePointPosition(Point point, Point e)        {            //MouseDrag 中心            if ((e.X == point.X) && (e.Y == point.Y))            {                return EnumMousePointPosition.MouseDrag;            }            //MouseSizeTopLeft 上左            if ((e.X < point.X) && (e.Y < point.Y))            {                return EnumMousePointPosition.MouseSizeTopLeft;            }            //MouseSizeTopRight 下右            if ((e.X > point.X) && (e.Y < point.Y))            {                return EnumMousePointPosition.MouseSizeTopRight;            }            //MouseSizeLeft 左侧            if ((e.X < point.X) && (e.Y == point.Y))            {                return EnumMousePointPosition.MouseSizeLeft;            }            //MouseSizeRight 右侧            if ((e.X > point.X) && (e.Y == point.Y))            {                return EnumMousePointPosition.MouseSizeRight;            }            //MouseSizeTop 上侧            if ((e.X == point.X) && (e.Y < point.Y))            {                return EnumMousePointPosition.MouseSizeTop;            }            //MouseSizeBottom 下侧            if ((e.X == point.X) && (e.Y > point.Y))            {                return EnumMousePointPosition.MouseSizeBottom;            }            //MouseSizeBottomRight 下左            if ((e.X < point.X) && (e.Y > point.Y))            {                return EnumMousePointPosition.MouseSizeBottomLeft;            }            //MouseSizeBottomRight 下右            if ((e.X > point.X) && (e.Y > point.Y))            {                return EnumMousePointPosition.MouseSizeBottomRight;            }            return EnumMousePointPosition.MouseSizeNone;        }        List<PointF> m_ThreePointCirclePoints = new List<PointF>();        protected override void OnMouseDown(MouseEventArgs e)        {            //lblFocus.Focus();            bool runflag = m_MeasureAppForm.MeasureThreadRunFlag;            Point _Point;            this.m_MouseDownPoint = new Point(e.X, e.Y);            //判断当前是否手绘            if (m_IsDrawMeasure && !runflag)            {                var measurepoint = new Rectangle(this.m_MouseDownPoint, new Size(2, 2));                if (m_visualStage.CheckMeasureAreaIsBeyondStageArea(measurepoint))                {                    m_IsDrawMeasure = true;                }                else                {                    m_IsDrawMeasure = false;                    return;                }                COTSSample workSample = m_MeasureAppForm.m_ProjParam.GetWorkSample();                if (workSample == null) return;                string sampleHoleName = workSample.GetSampleHoleName();                string sampleName = workSample.GetName();                //绘制多边形 测量区域                if (m_DrawMeasureType == DrawMeasureAreaActionType.Polygon)                {                    if (m_DrawMeasureGDIObject == null)                    {                        m_DrawMeasureGDIObject = new CMeasureArea(new List<PointF>(), ShapeType.POLYGON, sampleHoleName, sampleName, Color.Red);                    }                    var polygonPoints = m_DrawMeasureGDIObject.GetPolygonPointFList();                    if (m_IsDrawPolygonFinish)                    {                        DialogResult dResult = MessageBox.Show(table["message10"].ToString(), table["message11"].ToString(), MessageBoxButtons.YesNo);                        if (dResult == DialogResult.Yes)                        {                            Point startPoint = new Point((int)(polygonPoints[0].X), (int)(polygonPoints[0].Y));//the last point is the first point too.we get the first point as the last point.                            polygonPoints.Add(startPoint);                            CMeasureArea polygonGdi = new CMeasureArea(polygonPoints, ShapeType.POLYGON, sampleHoleName, sampleName, Color.Red);                            polygonGdi.SetZoomNumber(m_visualStage.GetZoomNum());                            polygonGdi.SetDisplayRefPoint(m_visualStage.GetDisplayRefPoint());                            polygonGdi.SetPolygonPointFList(polygonPoints);                            polygonGdi.IsWorkSample = true;                            var sam = GetVisualSampleByName(sampleName);                            if (sam == null) return;                            sam.SetMeasureGDIObject(polygonGdi);                            m_DrawPolygonFinishGDIObject = null;                            m_DrawMeasureGDIObject = null;                            var para = m_visualStage.GetSampleMeasurePara(sam.GetMeasureGDIObject());                            m_MeasureAppForm.m_ProjParam.SetWorkSampleHoleAndMeasureArea(para);                            m_MeasureAppForm.SwitchWSample(m_SampleSelectName);                            //-------------------- draw measrue field and save these data to sample                            PrepareMeasureField(sam, MSR_RUN_TYPE.RUNMEASURE);                            //--------------------                            Invalidate();                            m_IsDrawMeasure = false;                            m_IsDrawPolygonFinish = false;                            this.Cursor = Cursors.Default;                            return;                        }                        else                        {                            return;                        }                    }                    polygonPoints.Add(m_MouseDownPoint);                    m_DrawMeasureGDIObject.SetPolygonPointFList(polygonPoints);                    return;                }                else if (m_DrawMeasureType == DrawMeasureAreaActionType.CircleByThreePoints)                {                    if (m_DrawMeasureGDIObject == null)                    {                        m_DrawMeasureGDIObject = new CMeasureArea(new List<PointF>(), ShapeType.POLYGON, sampleHoleName, sampleName, Color.Red);                    }                    var m_ThreePointCirclePoints = m_DrawMeasureGDIObject.GetPolygonPointFList();                    m_ThreePointCirclePoints.Add(m_MouseDownPoint);                    if (m_ThreePointCirclePoints.Count == 3)//three point can make a circle .                    {                        float r;                        PointF Centerpoint;                        GetTriangleExcenterRadius(m_ThreePointCirclePoints[0], m_ThreePointCirclePoints[1], m_ThreePointCirclePoints[2], out r, out Centerpoint);                        RectangleF rectangleThree = new RectangleF(new PointF(Centerpoint.X - r, Centerpoint.Y - r), new SizeF(r * 2, r * 2));                        if (m_visualStage.CheckMeasureAreaIsBeyondStageArea(rectangleThree))                        {                            m_IsDrawMeasure = true;                        }                        else                        {                            m_IsDrawMeasure = false;                            return;                        }                        Color MeasureColor = Color.Red;                        CMeasureArea CRectangleGDI = new CMeasureArea(rectangleThree, ShapeType.CIRCLE, sampleHoleName, sampleName, MeasureColor);                        m_DrawMeasureGDIObject = CRectangleGDI;                        m_DrawMeasureGDIObject.SetZoomNumber(m_visualStage.GetZoomNum());                        m_DrawMeasureGDIObject.SetDisplayRefPoint(m_visualStage.GetDisplayRefPoint());                        var sam1 = GetVisualSampleByName(sampleName);                        if (sam1 == null) return;                        sam1.SetMeasureGDIObject(m_DrawMeasureGDIObject);                        sam1.GetMeasureGDIObject().SetZoomedRegionF(rectangleThree);                        m_DrawMeasureGDIObject = null;                        m_ThreePointCirclePoints.Clear();                        var para = m_visualStage.GetSampleMeasurePara(sam1.GetMeasureGDIObject());                        m_MeasureAppForm.m_ProjParam.SetWorkSampleHoleAndMeasureArea(para);                        m_MeasureAppForm.SetRunType(MSR_RUN_TYPE.RUNMEASURE);                        m_MeasureAppForm.SwitchWSample(m_SampleSelectName);                        Invalidate();                        //设置手绘标识                        m_IsDrawMeasure = false;                        this.Cursor = Cursors.Default;                        return;                    }                    Invalidate();                    return;                }                else if (m_DrawMeasureType == DrawMeasureAreaActionType.Rectangle)                {                    int sizeWH = 5;                    Size size = new Size(sizeWH, sizeWH);                    Rectangle rectangle = new Rectangle(m_MouseDownPoint, size);                    Color MeasureColor = Color.Red;                    CMeasureArea newGDI;                    newGDI = new CMeasureArea(rectangle, ShapeType.RECTANGLE, sampleName, sampleName, MeasureColor);                    newGDI.SetZoomNumber(m_visualStage.GetZoomNum());                    newGDI.SetDisplayRefPoint(m_visualStage.GetDisplayRefPoint());                    m_DrawMeasureGDIObject = newGDI;                    return;                }                else if (m_DrawMeasureType == DrawMeasureAreaActionType.Circle)                {                    int sizeWH = 5;                    Size size = new Size(sizeWH, sizeWH);                    Rectangle rectangle = new Rectangle(m_MouseDownPoint, size);                    Color MeasureColor = Color.Red;                    CMeasureArea newGDI;                    newGDI = new CMeasureArea(rectangle, ShapeType.CIRCLE, sampleName, sampleName, MeasureColor);                    newGDI.SetZoomNumber(m_visualStage.GetZoomNum());                    newGDI.SetDisplayRefPoint(m_visualStage.GetDisplayRefPoint());                    m_DrawMeasureGDIObject = newGDI;                    return;                }                else if (m_DrawMeasureType == DrawMeasureAreaActionType.InverseSelection)                {                    int sizeWH = 5;                    Size size = new Size(sizeWH, sizeWH);                    Rectangle rectangle = new Rectangle(m_MouseDownPoint, size);                    Color MeasureColor = Color.Red;                    CMeasureArea newGDI;                    newGDI = new CMeasureArea(rectangle, ShapeType.RECTANGLE, sampleName, sampleName, MeasureColor);                    newGDI.SetZoomNumber(m_visualStage.GetZoomNum());                    newGDI.SetDisplayRefPoint(m_visualStage.GetDisplayRefPoint());                    m_DrawMeasureGDIObject = newGDI;                    return;                }            }            else if (!m_IsDrawMeasure)            {                if (e.Button == MouseButtons.Right)                {                    if (m_MeasureAppForm.MeasureThreadRunFlag)                    {                        SetShowCMStrip(ContextMenuType.ThreadRunMenu, CMStrip);                        _Point = this.PointToClient(Cursor.Position);                        this.CMStrip.Show(this, _Point);                        return;                    }                    //获取鼠标右键点击位置                    m_MouseDownPoint = e.Location;                    var workingSam = GetWorkingVisualSample();                    if (workingSam != null)                    {                        foreach (CDisplayGDIObject item in workingSam.GetMeasureFieldGDIObjects())                        {                            if (item.IfZoomContains(e.Location))                            {                                _Point = this.PointToClient(Cursor.Position);                                SetShowCMStrip(ContextMenuType.FieldMenu, CMStrip);                                this.CMStrip.Show(this, _Point);                                return;                            }                        }                    }                    foreach (var item in m_visualSamples)                    {                        bool isSelectingMeasureArea = false;                        if (item.GetMeasureGDIObject().Myshape == ShapeType.POLYGON)                        {                            if (item.GetMeasureGDIObject().GetGPath().IsVisible(e.Location))                            {                                isSelectingMeasureArea = true;                            }                        }                        else                        {                            if (item.GetMeasureGDIObject().IfZoomContains(e.Location))                            {                                isSelectingMeasureArea = true;                                int WH = 2;                                Rectangle rect = new Rectangle(e.Location, new Size(WH, WH));                                if (!m_visualStage.CheckMeasureAreaIsBeyondStageArea(rect))                                {                                    break;                                }                            }                        }                        if (isSelectingMeasureArea)                        {                            SetShowCMStrip(ContextMenuType.MeasureMenu, CMStrip);                            _Point = this.PointToClient(Cursor.Position);                            this.CMStrip.Show(this, _Point);                            m_SampleHoleSelectName = item.GetMeasureGDIObject().HoleNo;                            if (m_SampleSelectName != item.GetMeasureGDIObject().SampleName)                            {                                m_SampleSelectName = item.GetSampleName();                                item.SetIsWorkSample(true);                                //设置工作样品                                m_MeasureAppForm.SwitchWSample(m_SampleSelectName);                                Invalidate();                            }                            return;                        }                    }                    foreach (CDisplayGDIObject item in m_visualStage.FrameOfHoleGDIObjects)                    {                        if (item.IfZoomContains(e.Location))                        {                            _Point = this.PointToClient(Cursor.Position);                            SetShowCMStrip(ContextMenuType.SampleHoleBSEImage, CMStrip);                            this.CMStrip.Show(this, _Point);                            return;                        }                    }                    //只点击样品上(测量区域被移动到其他地方)                    for (int i = m_visualSamples.Count - 1; i >= 0; i--)                    {                        if (m_visualSamples[i].GetSampleGDIObject().IfZoomContains(e.Location))                        {                            _Point = this.PointToClient(Cursor.Position);                            //设置编辑右键菜单项                            SetShowCMStrip(ContextMenuType.SampleMenu, CMStrip);                            this.CMStrip.Show(this, _Point);                            if (m_SampleSelectName != m_visualSamples[i].GetSampleName())                            {                                m_SampleSelectName = m_visualSamples[i].GetSampleName();                                m_SampleHoleSelectName = m_visualSamples[i].GetMeasureGDIObject().HoleNo;                                m_visualSamples[i].SetIsWorkSample(true);                                m_MeasureAppForm.SwitchWSample(m_SampleSelectName);                                Invalidate();                            }                            return;                        }                    }                    //点击样品孔                    CDisplayGDIObject gdiitem;                    if (m_visualStage.IfMouseInSampleHole(e.Location, out gdiitem))                    {                        _Point = this.PointToClient(Cursor.Position);                        SetShowCMStrip(ContextMenuType.SampleHoleMenu, CMStrip);                        this.CMStrip.Show(this, _Point);                        m_SampleHoleSelectName = gdiitem.HoleNo;                        return;                    }                    if (m_visualStage.IfMouseInStage(e.Location))                    {                        SetShowCMStrip(ContextMenuType.SampleStateMenu, CMStrip);                        _Point = this.PointToClient(Cursor.Position);                        this.CMStrip.Show(this, _Point);                        return;                    }                    return;                }                else if (e.Button == MouseButtons.Left)                {                    //测量区域                    if (Control.ModifierKeys == Keys.Control)// ctrl key is pressed,means changing position or altering the visual sample area.                    {                        var sam = GetWorkingVisualSample();                        if (sam != null)                        {                            var item = sam.GetMeasureGDIObject();                            var selWSampleMeasureStatus = m_MeasureAppForm.m_ProjParam.GetWorkSampleMeasureStatus();                            if (selWSampleMeasureStatus)                            {                                return;                            }                            if (item.Myshape == ShapeType.POLYGON)                            {                                bool isSelPolygonPoint = false;                                foreach (PointF itemPoint in item.GetPolygonPointFList())                                {                                    if (Math.Abs(itemPoint.X - e.X) <= m_OffsetPX && Math.Abs(itemPoint.Y - e.Y) <= m_OffsetPX)                                    {                                        isSelPolygonPoint = true;                                        item.SelColor = Color.Red;                                        item.DraggingPoint = new PointF(itemPoint.X, itemPoint.Y);//e.Location;                                        item.IsAltering = true;//this state means operator want to alter the size of the polygon shape.                                        item.IsDragging = false;                                        break;                                    }                                }                                if (!isSelPolygonPoint)//not selecting the polygon point,just dragging on the inside area of the polygon shape.                                {                                    if (item.GetGPath().IsVisible(e.Location))                                    {                                        item.SelColor = Color.Red;                                        item.IsDragging = true;                                        item.DraggingPoint = e.Location;                                        item.IsAltering = false;                                    }                                }                            }                            else                            {                                if (item.GetGPath().IsOutlineVisible((float)e.X, (float)e.Y, new Pen(Color.Gray, m_PenWidth)))                                {                                    item.SelColor = Color.Red;                                    item.IsDragging = false;                                    item.DraggingPoint = e.Location;                                    item.IsAltering = true;                                }                                else//not outlinevisible then is altering sample area position.                                {                                    if (item.IfZoomContains(e.Location))                                    {                                        item.SelColor = Color.Red;                                        item.IsDragging = true;                                        item.DraggingPoint = e.Location;                                        item.IsAltering = false;                                    }                                }                            }                        }                    }                    //click on the sample area without ctrl key,just switch to that sample.                    if (!(Control.ModifierKeys == Keys.Control) && !runflag)                    {                        for (int i = m_visualSamples.Count - 1; i >= 0; i--)                        {                            var sam1 = m_visualSamples[i];                            if (sam1.GetSampleGDIObject().IfZoomContains(e.Location) || sam1.GetMeasureGDIObject().IfZoomContains(e.Location))                            {                                //NLog.LogManager.GetCurrentClassLogger().Info(sam1.GetMeasureGDIObject().GetOrigionalDrawRegionF().ToString());                                var measureItem = sam1.GetMeasureGDIObject();                                measureItem.SelColor = Color.Red;                                //设置为工作样品                                sam1.SetIsWorkSample(true);                                //获取颜色                                var theColor = OTSSamplespaceGraphicsPanelFun.GetColorValue(ColorType.SampleSelColor);                                sam1.GetSampleGDIObject().SelColor = theColor;                                m_SampleSelectName = sam1.GetSampleName();                                m_MeasureAppForm.SwitchWSample(m_SampleSelectName);                                break;                            }                        }                    }                    CDisplayGDIObject stageobj;                    if (m_visualStage.IfMouseInStage(e.Location, out stageobj))                    {                        MouseDownFunction(m_visualStage.GetAllGDIObject(), e);                        //样品                        MouseDownFunction(GetAllVisualSampleGDIObject(), e);                    }                }            }            //重绘控件            Invalidate();        }        protected override void OnMouseMove(MouseEventArgs e)        {            bool runflag = m_MeasureAppForm.MeasureThreadRunFlag;            if (runflag)            {                return;            }            this.m_MouseMovePoint = new Point(e.X, e.Y);            if (m_IsDrawMeasure && !runflag)            {                if (m_DrawMeasureGDIObject == null) return;//the object must be initialized by mousedown event.                //绘制多边形                if (m_DrawMeasureType == DrawMeasureAreaActionType.Polygon)                {                    m_DrawMeasureGDIObject.PolygonDrawingEndPoint = this.m_MouseMovePoint;                    var m_PolygonPoint = m_DrawMeasureGDIObject.GetPolygonPointFList();                    //显示直线开始点与鼠标当前位置的直线                    PointF startPoint = new PointF();                    if (m_PolygonPoint.Count > 0)                    {                        startPoint = m_PolygonPoint[0];                    }                    //显示绘制多边形完成标识                    if (m_PolygonPoint.Count > m_PolygonMinPointCount)                    {                        if (Math.Abs(m_MouseMovePoint.X - startPoint.X) <= m_OffsetPX && Math.Abs(m_MouseMovePoint.Y - startPoint.Y) <= m_OffsetPX)                        {                            //显示绘制多边形完成标识                            m_IsDrawPolygonFinish = true;                            int rectSize = m_MinMeasureWidthValue;                            if (m_DrawPolygonFinishGDIObject == null)                            {                                Point PolygonFinishPoint = new Point((int)(startPoint.X - m_MinMeasureWidthValue / 2), (int)(startPoint.Y - m_MinMeasureWidthValue / 2));                                Rectangle pRect = new Rectangle(PolygonFinishPoint, new Size(rectSize, rectSize));                                CDisplayGDIObject createPolygonFinish = new CDisplayGDIObject(pRect, GDIType.DrawPolygonFinish, "", "", Color.Green);                                m_DrawPolygonFinishGDIObject = createPolygonFinish;                            }                        }                        else                        {                            m_DrawPolygonFinishGDIObject = null;                            m_IsDrawPolygonFinish = false;                        }                    }                }                //圆形三点法                else if (m_DrawMeasureType == DrawMeasureAreaActionType.CircleByThreePoints)                {                    //m_ThreePointCirclePoints[m_ThreePointCirclePoints.Count-1] = this.m_MouseMovePoint;                    m_DrawMeasureGDIObject.PolygonDrawingEndPoint = this.m_MouseMovePoint;                }                else                {                    if (m_DrawMeasureGDIObject == null)                    {                        return;                    }                    RectangleF rectangle = m_DrawMeasureGDIObject.GetZoomedRegion;                    int tmp = (int)Math.Sqrt((m_MouseDownPoint.X - m_MouseMovePoint.X) * (m_MouseDownPoint.X - m_MouseMovePoint.X) + (m_MouseDownPoint.Y - m_MouseMovePoint.Y) * (m_MouseDownPoint.Y - m_MouseMovePoint.Y));                    if (tmp == 0)                    {                        tmp = m_DValue;                    }                    EnumMousePointPosition enumMouse = MousePointPosition(m_MouseDownPoint, m_MouseMovePoint);                    Point LTPoint = new Point();                    Size RBSize = new Size();                    switch (enumMouse)                    {                        case EnumMousePointPosition.MouseSizeTopLeft:                            if (m_DrawMeasureType == DrawMeasureAreaActionType.Circle)                            {                                LTPoint.X = m_MouseDownPoint.X - tmp;                                LTPoint.Y = m_MouseDownPoint.Y - tmp;                                RBSize.Width = tmp;                                RBSize.Height = tmp;                                rectangle = new Rectangle(LTPoint, RBSize);                            }                            else                            {                                LTPoint.X = m_MouseMovePoint.X;                                LTPoint.Y = m_MouseMovePoint.Y;                                RBSize.Width = m_MouseDownPoint.X - m_MouseMovePoint.X;                                RBSize.Height = m_MouseDownPoint.Y - m_MouseMovePoint.Y;                                rectangle = new Rectangle(LTPoint, RBSize);                            }                            break;                        case EnumMousePointPosition.MouseSizeTopRight:                            if (m_DrawMeasureType == DrawMeasureAreaActionType.Circle)                            {                                LTPoint.X = m_MouseDownPoint.X;                                LTPoint.Y = m_MouseDownPoint.Y - tmp;                                RBSize.Width = tmp;                                RBSize.Height = tmp;                                rectangle = new Rectangle(LTPoint, RBSize);                            }                            else                            {                                LTPoint.X = m_MouseDownPoint.X;                                LTPoint.Y = m_MouseDownPoint.Y - (m_MouseDownPoint.Y - m_MouseMovePoint.Y);                                RBSize.Width = m_MouseMovePoint.X - m_MouseDownPoint.X;                                RBSize.Height = m_MouseDownPoint.Y - m_MouseMovePoint.Y;                                rectangle = new Rectangle(LTPoint, RBSize);                            }                            break;                        case EnumMousePointPosition.MouseSizeBottomLeft:                            if (m_DrawMeasureType == DrawMeasureAreaActionType.Circle)                            {                                LTPoint.X = m_MouseDownPoint.X - tmp;                                LTPoint.Y = m_MouseDownPoint.Y;                                RBSize.Width = tmp;                                RBSize.Height = tmp;                                rectangle = new Rectangle(LTPoint, RBSize);                            }                            else                            {                                LTPoint.X = m_MouseDownPoint.X - (m_MouseDownPoint.X - m_MouseMovePoint.X);                                LTPoint.Y = m_MouseDownPoint.Y;                                RBSize.Width = m_MouseDownPoint.X - m_MouseMovePoint.X;                                RBSize.Height = m_MouseMovePoint.Y - m_MouseDownPoint.Y;                                rectangle = new Rectangle(LTPoint, RBSize);                            }                            break;                        case EnumMousePointPosition.MouseSizeBottomRight:                            if (m_DrawMeasureType == DrawMeasureAreaActionType.Circle)                            {                                LTPoint.X = m_MouseDownPoint.X;                                LTPoint.Y = m_MouseDownPoint.Y;                                RBSize.Width = tmp;                                RBSize.Height = tmp;                                rectangle = new Rectangle(LTPoint, RBSize);                            }                            else                            {                                LTPoint.X = m_MouseDownPoint.X;                                LTPoint.Y = m_MouseDownPoint.Y;                                RBSize.Width = m_MouseMovePoint.X - m_MouseDownPoint.X;                                RBSize.Height = m_MouseMovePoint.Y - m_MouseDownPoint.Y;                                rectangle = new Rectangle(LTPoint, RBSize);                            }                            break;                    }                    //在样品台内部缩放                    if (m_visualStage.CheckMeasureAreaIsBeyondStageArea(rectangle))                    {                        var rec = new Rectangle(LTPoint, RBSize);                        m_DrawMeasureGDIObject.SetZoomedRegionF(new RectangleF(new PointF((rec.X), (rec.Y)), new SizeF(rec.Width, rec.Height)));                    }                }            }            if ((Control.ModifierKeys == Keys.Control) && !runflag)//is dragging on the edge or on the vertex so we can alter the size of the item.            {                var sam = GetWorkingVisualSample();                if (sam == null) return;                var curMeasureGdi = sam.GetMeasureGDIObject();                if (curMeasureGdi.IsAltering)//click on the  edge or on the polygon point(or vertex point of rectangle).                {                    if (curMeasureGdi.Myshape == ShapeType.POLYGON)                    {                        for (int i = 0; i < curMeasureGdi.GetPolygonPointFList().Count; i++)                        {                            if (curMeasureGdi.GetPolygonPointFList()[i].X == curMeasureGdi.DraggingPoint.X && curMeasureGdi.GetPolygonPointFList()[i].Y == curMeasureGdi.DraggingPoint.Y)                            {                                //移动点的位置                                RectangleF selPolygonPointRect = new RectangleF(0, 0, 2, 2);                                selPolygonPointRect.X = curMeasureGdi.DraggingPoint.X + e.X - curMeasureGdi.DraggingPoint.X;                                selPolygonPointRect.Y = curMeasureGdi.DraggingPoint.Y + e.Y - curMeasureGdi.DraggingPoint.Y;                                //判断样品是否超越样品台                                if (m_visualStage.CheckMeasureAreaIsBeyondStageArea(selPolygonPointRect))                                {                                    List<PointF> polygonPs = curMeasureGdi.GetPolygonPointFList();                                    if (i == 0 || i == curMeasureGdi.GetPolygonPointFList().Count - 1)                                    {                                        curMeasureGdi.DraggingPoint = new Point(e.X, e.Y);                                        polygonPs[0] = curMeasureGdi.DraggingPoint;                                        polygonPs[curMeasureGdi.GetPolygonPointFList().Count - 1] = curMeasureGdi.DraggingPoint;//the last point and the first point of one polygon object are the same.                                    }                                    else                                    {                                        curMeasureGdi.DraggingPoint = new Point(e.X, e.Y);                                        polygonPs[i] = curMeasureGdi.DraggingPoint;                                    }                                    curMeasureGdi.SetPolygonPointFList(polygonPs);                                    curMeasureGdi.DraggingPoint = e.Location;                                    break;                                }                            }                        }                    }                    else                    {                        if (curMeasureGdi.Myshape == ShapeType.RECTANGLE)                        {                            List<PointF> vertexPoint = curMeasureGdi.GetVertexPoints();                            var oldRegion = curMeasureGdi.GetZoomedRegionF();                            for (int i = 0; i < vertexPoint.Count; i++)                            {                                var itemPoint = vertexPoint[i];                                PointF lefttop, leftBottom, rightTop, rightBottom;                                float width;                                float height;                                if (Math.Abs(itemPoint.X - e.X) <= m_OffsetPX && Math.Abs(itemPoint.Y - e.Y) <= m_OffsetPX)                                {                                    switch (i)                                    {                                        case 0://left top point                                            lefttop = new PointF(e.X, e.Y);                                            width = vertexPoint[2].X - e.X;                                            height = vertexPoint[1].Y - e.Y;                                            curMeasureGdi.SetZoomedRegionF(new RectangleF(lefttop, new SizeF(width, height)));                                            break;                                        case 1://left bottom                                            leftBottom = new PointF(e.X, e.Y);                                            width = vertexPoint[3].X - e.X;                                            height = -(vertexPoint[0].Y - e.Y);                                            curMeasureGdi.SetZoomedRegionF(new RectangleF(new PointF(leftBottom.X, leftBottom.Y - height), new SizeF(width, height)));                                            break;                                        case 2://right top                                             rightTop = new PointF(e.X, e.Y);                                            width = -(vertexPoint[0].X - e.X);                                            height = (vertexPoint[3].Y - e.Y);                                            curMeasureGdi.SetZoomedRegionF(new RectangleF(new PointF(rightTop.X - width, rightTop.Y), new SizeF(width, height)));                                            break;                                        case 3://right bottom                                            rightBottom = new PointF(e.X, e.Y);                                            width = (e.X - vertexPoint[0].X);                                            height = (e.Y - vertexPoint[0].Y);                                            curMeasureGdi.SetZoomedRegionF(new RectangleF(new PointF(rightBottom.X - width, rightBottom.Y - height), new SizeF(width, height)));                                            break;                                    }                                    Invalidate();                                }                            }                        }                        else                        {                            PointF gdicenter = m_visualStage.GetCenterPoint(curMeasureGdi.GetZoomedRegion);                            //获取鼠标坐标                            PointF mousePoint = e.Location;                            RectangleF rectTemp;                            //计算偏离半径                            float a = (gdicenter.X - mousePoint.X) * (gdicenter.X - mousePoint.X);                            float b = (gdicenter.Y - mousePoint.Y) * (gdicenter.Y - mousePoint.Y);                            float c = a + b;                            double r = System.Math.Sqrt(c);                            float width = (float)r * 2;                            rectTemp = new RectangleF(gdicenter.X - (float)r, gdicenter.Y - (float)r, width, width);                            if (m_visualStage.CheckMeasureAreaIsBeyondStageArea(rectTemp))                            {                                //如果大于最小测量高度 设置图形大小                                if (rectTemp.Width / curMeasureGdi.GetZoomNumber() > m_MinMeasureWidthValue)                                {                                    curMeasureGdi.SetZoomedRegionF(rectTemp);                                    curMeasureGdi.DraggingPoint = e.Location;                                }                            }                            else                            {                                return;                            }                            Invalidate();                            return;                        }                    }                }                else if (curMeasureGdi.IsDragging)                {                    if (curMeasureGdi.GetGPath().IsVisible(e.Location))                    {                        if (m_visualStage.GetEdgeGDIObj().GetGPath().IsVisible(e.Location))                        {                            curMeasureGdi.PositionAltering(e.Location, true);                        }                    }                }            }            else            {                //样品台                MouseMoveFucntion(m_visualStage.GetAllGDIObject(), e);                //样品                MouseMoveFucntion(GetAllVisualSampleGDIObject(), e);                //样品孔BSE图片                MouseMoveFucntion(m_visualStage.FrameOfHoleGDIObjects, e);            }            //显示XY轴            m_visualStage.ShowSemCoordvAL(e.Location, m_MeasureAppForm);            Invalidate();        }        protected override void OnMouseUp(MouseEventArgs e)        {            //如果正在进行测量线程,则退出鼠标点击操作            var runflag = m_MeasureAppForm.MeasureThreadRunFlag;            var sam = GetWorkingVisualSample();            //判断当前是否手绘            if (m_IsDrawMeasure && !runflag)            {                if (m_DrawMeasureType == DrawMeasureAreaActionType.InverseSelection)                {                    var workspl = m_MeasureAppForm.m_ProjParam.GetWorkSample();                    foreach (var gdi in sam.GetMeasureFieldGDIObjects())                    {                        if (m_DrawMeasureGDIObject.IfContains(new Point((int)gdi.GetCenterPoint().X, (int)gdi.GetCenterPoint().Y)))                        {                            gdi.Enable = !gdi.Enable;                            workspl.SetFieldEnable(new Point(gdi.OTSX, gdi.OTSY), gdi.Enable);                        }                    }                    m_DrawMeasureGDIObject = null;                    this.Cursor = Cursors.Default;                    //设置手绘标识                    m_IsDrawMeasure = false;                }                else if (m_DrawMeasureType == DrawMeasureAreaActionType.Rectangle || m_DrawMeasureType == DrawMeasureAreaActionType.Circle)                {                    //var item = m_DrawMeasureGDIObject;                    m_DrawMeasureGDIObject.HoleNo = sam.GetMeasureGDIObject().HoleNo;                    m_DrawMeasureGDIObject.IsWorkSample = sam.GetMeasureGDIObject().IsWorkSample;                    sam.GetMeasureFieldGDIObjects().Clear();                    sam.SetMeasureGDIObject(m_DrawMeasureGDIObject);                    var para = m_visualStage.GetSampleMeasurePara(m_DrawMeasureGDIObject);                    m_MeasureAppForm.m_ProjParam.SetWorkSampleHoleAndMeasureArea(para);                    m_MeasureAppForm.SetRunType(MSR_RUN_TYPE.RUNMEASURE);                    PrepareMeasureField(sam, MSR_RUN_TYPE.RUNMEASURE);                    //according to the calculated fields outermost border rectangle update the measure area.                    if (m_DrawMeasureType == DrawMeasureAreaActionType.Rectangle)                    {                        var rec = sam.GetAllGDIObjectsRectangle();                        m_DrawMeasureGDIObject.SetZoomedRegionF(rec);                        sam.SetMeasureGDIObject(m_DrawMeasureGDIObject);                        para = m_visualStage.GetSampleMeasurePara(m_DrawMeasureGDIObject);                        m_MeasureAppForm.m_ProjParam.SetWorkSampleHoleAndMeasureArea(para);                    }                                            //-------------                    m_DrawMeasureGDIObject = null;                    this.Cursor = Cursors.Default;                    //设置手绘标识                    m_IsDrawMeasure = false;                }            }            if (sam != null && !runflag)            {                var measureItem = sam.GetMeasureGDIObject();                if (measureItem.IsDragging || measureItem.IsAltering)//while pressing ctrl key then it must be altering something(position or size)                {                    var selWSampleMeasureStatus = m_MeasureAppForm.m_ProjParam.GetWorkSampleMeasureStatus();                    if (!selWSampleMeasureStatus)                    {                        var para = m_visualStage.GetSampleMeasurePara(measureItem);                        m_MeasureAppForm.m_ProjParam.SetWorkSampleHoleAndMeasureArea(para);//set the altering result to the public parameter object                        this.Cursor = System.Windows.Forms.Cursors.Default;                        PrepareMeasureField(sam, m_MeasureAppForm.GetRunType());                    }                    measureItem.IsAltering = false;                    measureItem.IsDragging = false;                    Invalidate();                }            }            //only moving the whole stage            MouseUpFucntion(m_visualStage.GetAllGDIObject(), e);            //样品             MouseUpFucntion(GetAllVisualSampleGDIObject(), e);        }        public void DrawSampleHoleImage(CDisplayGDIObject img)        {            var m_RegionF = img.GetOrigionalDrawRegionF();            PointF startPointF = new PointF(m_RegionF.Left, m_RegionF.Top);            PointF endPointF = new PointF(m_RegionF.Right + 2, m_RegionF.Bottom + 2);            //绘制样品孔中图像信息            if (img.BSEImage != null)            {                Image thumbnail = img.BSEImage.GetThumbnailImage((int)m_RegionF.Width + 2, (int)m_RegionF.Height + 2, null, IntPtr.Zero);                var g = Graphics.FromImage(sampleHoleImageBuffer.BSEImage);                              g.DrawImage(thumbnail, startPointF);            }        }        protected override void OnPaint(PaintEventArgs e)//处理重绘情况        {            OnPaint(e, m_visualStage.GetAllGDIObject());            OnPaint(e, GetOnlyVisualSampleGDIObject());            if (IsShowSampleHoleImage)            {                //e.Graphics.DrawImage(sampleHoleImageBuffer.BSEImage, sampleHoleImageBuffer.GetOrigionalDrawRegionF());                if (sampleHoleImageBuffer.BSEImage != null)                {                    e.Graphics.DrawImage(sampleHoleImageBuffer.BSEImage, sampleHoleImageBuffer.GetZoomedRegionF());                }                OnPaint(e,m_visualStage.FrameOfHoleGDIObjects);            }        }        public void OnPaint(PaintEventArgs e, List<CDisplayGDIObject> m_ObjectGDIObjects)//处理重绘情况        {            foreach (CDisplayGDIObject item in m_ObjectGDIObjects.ToArray())            {                if (item != null)                {                    item.OnPaint(e);                }            }        }        protected override void OnMouseWheel(MouseEventArgs e)        {            bool runflag = m_MeasureAppForm.MeasureThreadRunFlag;            if (runflag)            {                return;            }            var currentZoomNum = m_visualStage.GetZoomNum();            //如果正在进行测量线程,则退出鼠标点击操作            if (e.Delta > 0)            {                #region 计算标尺尺寸部份-----放大------                //计算标尺实际占用像素的宽度                f_ruler_size = f_old_ruler_size * currentZoomNum;                control_Ruler2.RulerWidth = Convert.ToInt32(f_ruler_size);                control_Ruler2.Value = 1;                //放大过长时,这里将宽度与代表的实际宽度进行折半显示                while (control_Ruler2.Width > 300)                {                    control_Ruler2.RulerWidth = Convert.ToInt32(control_Ruler2.RulerWidth) / 2;                    control_Ruler2.Value = control_Ruler2.Value * 2;                }                control_Ruler2.SetValue(control_Ruler2.RulerWidth, control_Ruler2.Value.ToString());                #endregion            }            else            {                if (currentZoomNum >= f_zoom_mix)                {                    #region 计算标尺尺寸部份-----缩小                    f_ruler_size = f_old_ruler_size * currentZoomNum;                    control_Ruler2.RulerWidth = Convert.ToInt32(f_ruler_size);                    control_Ruler2.Value = 1;                    while (control_Ruler2.Width > 300)                    {                        control_Ruler2.RulerWidth = Convert.ToInt32(control_Ruler2.RulerWidth) / 2;                        control_Ruler2.Value = control_Ruler2.Value * 2;                    }                    control_Ruler2.SetValue(control_Ruler2.RulerWidth, control_Ruler2.Value.ToString());                    #endregion                }            }            //设置记录每次比例缩放后的总体比例系数            if (e.Delta > 0)            {                currentZoomNum += 0.1f;            }            else            {                currentZoomNum -= 0.1f;                if (currentZoomNum < 1)                {                    currentZoomNum = 1;                }                if (currentZoomNum == 1)                {                    //标尺恢复初始                    f_ruler_size = f_old_ruler_size;                    control_Ruler2.RulerWidth = Convert.ToInt32(f_ruler_size);                    control_Ruler2.ShowString = "1mm";                    control_Ruler2.SetValue(control_Ruler2.RulerWidth);                }            }            MouseWheelFunction(GetAllVisualSampleGDIObject(), e.Location, currentZoomNum);            //样品孔            MouseWheelFunction(m_visualStage.GetAllGDIObject(), e.Location, currentZoomNum);            base.OnMouseWheel(e);            //重新绘制            Invalidate();        }        private void MouseWheelFunction(List<CDisplayGDIObject> objList, Point mousePoint, float ZoomNum)        {            for (int i = 0; i < objList.Count; i++)            {                objList[i].Zoom(mousePoint, ZoomNum);            }        }        #endregion        #region 鼠标方法操作        public void MouseDownFunction(List<CDisplayGDIObject> objList, MouseEventArgs e)        {            foreach (CDisplayGDIObject item in objList)            {                item.IsMoving = true;                item.DraggingPoint = e.Location;            }        }        public void MouseMoveFucntion(List<CDisplayGDIObject> objList, MouseEventArgs e)        {            foreach (CDisplayGDIObject item in objList)            {                if (item.IsMoving)                {                    item.Move(e.Location);                }            }        }        public void MouseUpFucntion(List<CDisplayGDIObject> objList, MouseEventArgs e)        {            foreach (CDisplayGDIObject item in objList)            {                if (item.IsDragging)                {                    item.IsDragging = false;                    item.DraggingPoint = Point.Empty;                }                if (item.IsMoving)                {                    item.IsMoving = false;                    item.DraggingPoint = Point.Empty;                }            }        }        #endregion        private void DeleteSampleInfo(string sampleSelectName)        {            if (ShowDeleteDialog(sampleSelectName))            {                //设置当前状态                m_MeasureAppForm.SetRunType(MSR_RUN_TYPE.RUNMEASURE);                m_MeasureAppForm.DeleteSample(sampleSelectName);            }            return;        }        private bool ShowDeleteDialog(string sampleName)        {            //国际化            OTSCommon.Language lan = new OTSCommon.Language();            Hashtable table = lan.GetNameTable("OTSIncAMeasureAppForm");            string str1 = table["message5"].ToString();            string str2 = table["message6"].ToString();            string sDeleteSampleName = str1;            sDeleteSampleName += sampleName;            sDeleteSampleName += "  信息 ?";            if (DialogResult.OK == MessageBox.Show(sDeleteSampleName, "Delete sample information prompt", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning))            {                return true;            }            else            {                return false;            }        }        public bool setSlopFocusParam(_5_OTSMeasureStatuImageFun.SlopFocus slopFocus)        {            if (slopFocus == null)            {                return false;            }            else            {                m_MeasureAppForm.m_ProjParam.GetWorkSample().GetMsrParams().SlopParam = slopFocus.GetCSlopFocusParam();                return true;            }        }        protected override bool ProcessDialogKey(Keys keyData)        {            if (keyData == Keys.Up || keyData == Keys.Down || keyData == Keys.Left || keyData == Keys.Right)            {                return false;            }            else            {                return base.ProcessDialogKey(keyData);            }        }        public void OTSSamplespaceWindow_KeyDown(object sender, KeyEventArgs e)        {            //鼠标相对于屏幕的坐标            Point p1 = MousePosition;            //鼠标相对于窗体的坐标            Point mousePoint = this.PointToClient(p1);            Pen GrayPen = new Pen(Color.Gray, m_PenWidth);            //设置鼠标在样品测量区域上的样式            foreach (var sam in m_visualSamples)            {                var item = sam.GetMeasureGDIObject();                if (item.GetGPath() != null)                {                    if (item.Myshape == ShapeType.POLYGON)                    {                        //在多边形点附近切换光标                        foreach (PointF itemPoint in item.GetPolygonPointFList())                        {                            if (Math.Abs(itemPoint.X - mousePoint.X) <= m_OffsetPX && Math.Abs(itemPoint.Y - mousePoint.Y) <= m_OffsetPX)                            {                                if (Control.ModifierKeys == Keys.Control)                                {                                    this.Cursor = System.Windows.Forms.Cursors.Hand;                                    return;                                }                            }                        }                        if (item.GetGPath().IsVisible(mousePoint))                        {                            if (Control.ModifierKeys == Keys.Control)                            {                                this.Cursor = System.Windows.Forms.Cursors.SizeAll;                                return;                            }                        }                    }                    else                    {                        if (item.GetGPath().IsOutlineVisible(mousePoint, GrayPen))                        {                            if (item.Myshape == ShapeType.RECTANGLE)                            {                                List<PointF> vertexPoint = item.GetVertexPoints();                                foreach (PointF itemPoint in vertexPoint)                                {                                    if (Math.Abs(itemPoint.X - mousePoint.X) <= m_OffsetPX && Math.Abs(itemPoint.Y - mousePoint.Y) <= m_OffsetPX)                                    {                                        if (Control.ModifierKeys == Keys.Control)                                        {                                            this.Cursor = System.Windows.Forms.Cursors.Hand;                                            return;                                        }                                    }                                }                            }                            else                            {                                if (Control.ModifierKeys == Keys.Control)                                {                                    this.Cursor = System.Windows.Forms.Cursors.NoMove2D;                                    return;                                }                            }                        }                        if (item.IfZoomContains(mousePoint))                        {                            if (Control.ModifierKeys == Keys.Control)                            {                                this.Cursor = System.Windows.Forms.Cursors.SizeAll;                                return;                            }                        }                    }                }            }            //设置鼠标在样品上的样式            foreach (var sam in m_visualSamples)            {                var item = sam.GetSampleGDIObject();                if (!item.SelColor.Name.Equals("Transparent"))                {                    if (item.IfZoomContains(mousePoint))                    {                        if ((Control.ModifierKeys == Keys.Alt))                        {                            this.Cursor = System.Windows.Forms.Cursors.SizeAll;                            return;                        }                    }                }            }            if (e.KeyCode == Keys.Escape && m_IsDrawMeasure == true && m_DrawMeasureType == DrawMeasureAreaActionType.Polygon)            {                var polygonPoints = m_DrawMeasureGDIObject.GetPolygonPointFList();                if (polygonPoints.Count > 0)                {                    polygonPoints.RemoveAt(polygonPoints.Count - 1);//remove the last one.                    m_DrawMeasureGDIObject.SetPolygonPointFList(polygonPoints);                }                else                {                    m_IsDrawMeasure = false;                }            }            else if (e.KeyCode == Keys.Escape && m_IsDrawMeasure == true)            {                m_IsDrawMeasure = false;            }        }        private void OTSSamplespaceWindow_KeyUp(object sender, KeyEventArgs e)        {            //键盘抬起时,将选择区域的标识 设置为未选择状态            this.Cursor = System.Windows.Forms.Cursors.Default;            this.Invalidate();        }        public void KeyUps()        {            this.Cursor = System.Windows.Forms.Cursors.Default;        }        #region 处理方法总汇        #region 清除所需图形        public void ClearDrawInfo()        {            try            {                m_visualStage.FrameOfHoleGDIObjects.Clear();                m_visualStage.cleargdiobj();                var sam = GetWorkingVisualSample();                if (sam != null)                {                    if (m_DrawMeasureGDIObject != null)                    {                        m_DrawMeasureGDIObject = null;                    }                }                m_visualSamples.Clear();            }            catch (Exception ex)            {                log.Error(ex.ToString());            }        }        #endregion        #endregion        #region 绘制样品以及测量区域        /// <summary>        /// 绘制样品以及测量区域        /// </summary>        /// <param name="SMeasrueAreaList">测量区域的集合</param>        public void DrawSampleAndMeasureInfo(SampleMeasurePara SMeasrueArea)        {            var item = m_visualStage.GetHoleGDIByHoleName(SMeasrueArea.sampleHoleName);            if (item != null)            {                CVisualSampleArea sam;                if (InitVisualSample(SMeasrueArea.sampleHoleName, SMeasrueArea.sSampleName, out sam))                {                    if (SMeasrueArea.iShape == ShapeType.POLYGON)                    {                        var ps = new List<PointF>();                        foreach (var p in SMeasrueArea.DrawPolygonPointList)                        {                            var ctrlcoordPoint = m_visualStage.OTSCoordToCtrlCoord(p);                            ps.Add(ctrlcoordPoint);                        }                        CMeasureArea polygongdi = new CMeasureArea(ps, SMeasrueArea.iShape, SMeasrueArea.sampleHoleName, SMeasrueArea.sSampleName, Color.Red);                        sam.SetMeasureGDIObject(polygongdi);                        PrepareMeasureField(sam, m_MeasureAppForm.GetRunType());                    }                    else                    {                        PointF xHole = new PointF(SMeasrueArea.MeasureRect.Left, SMeasrueArea.MeasureRect.Top);                        PointF yHole = new PointF(SMeasrueArea.MeasureRect.Right, SMeasrueArea.MeasureRect.Bottom);                        CDisplayGDIObject gdi = m_visualStage.GetCtrlCoordGDI(xHole, yHole, m_visualStage. ConvertShapeTypeToGDIType (SMeasrueArea.iShape), "", "");                        gdi.SampleName = SMeasrueArea.sSampleName;                        gdi.HoleNo = SMeasrueArea.sampleHoleName;                        CMeasureArea measureGdi;                        if (m_visualStage.GetMeasureGdiObjectFromSampleGdi(gdi, out measureGdi))                        {                            sam.SetMeasureGDIObject(measureGdi);                            PrepareMeasureField(sam, m_MeasureAppForm.GetRunType());                        }                    }                    COTSMsrPrjResultData cProjMgr = m_MeasureAppForm.m_ProjParam.GetResultData();                    List<PointF> CompleteFieldList = cProjMgr.GetSampleByName(SMeasrueArea.sSampleName).GetMsrStatus().GetCompletedFieldsCenter();                    if (CompleteFieldList.Count > 0)                    {                        ChangeCompleteFieldMeasureState(CompleteFieldList, Color.Green, sam);                    }                }            }        }        #endregion        #region 获取帧图信息        public void ReadSEMDataAndSetToCurrentSample()        {            List<double> semParameters;            #region 获取电镜参数            ////获取Field操作类对象            if (m_SEMDATAFieldManage == null)            {                m_SEMDATAFieldManage = new SEMDATAFieldManage(m_MeasureAppForm.m_ProjParam);            }            //判断是否连接电镜            if (m_SEMDATAFieldManage.InitAndConnection())            {                //获取电镜参数 放大倍数与工作距离 等参数                semParameters = m_SEMDATAFieldManage.GetSEMMagAndWDParameter();                double mag = semParameters[0];                double wd = semParameters[1];                double bri = semParameters[2];                double contra = semParameters[3];                double kv = semParameters[4];                if (mag != 0 && wd != 0)                {                    //设置样品文件中的电镜参数                     m_MeasureAppForm.m_ProjParam.SetWorkingSampleSEMData(wd, mag, bri, contra, kv);                    //将右键SEM数据菜单设置为可编辑状态                    CMStrip.Items[(int)MenuIndex.SetSemData].Enabled = true;                }            }            #endregion        }        #region 获取Filed帧图        public List<CVisualFieldGDIObject> GetOTSFieldVisualRects(CVisualSampleArea sam, MSR_RUN_TYPE runType)        {            List<Point> listCenterPoint = new List<Point>();            Size rectSize = new Size();            var m_ResultData = m_MeasureAppForm.m_ProjParam.GetResultData();            var sample = m_ResultData.GetSampleByName(sam.GetSampleName());            List<CVisualFieldGDIObject> visualfieldRects = new List<CVisualFieldGDIObject>();            if (runType == MSR_RUN_TYPE.RUNSampleHoleImage)            {                InitHoleImgPosData(sample, out listCenterPoint, out rectSize);            }            else if (runType == MSR_RUN_TYPE.RUNMEASURE)            {                sample.InitFieldPosData(out listCenterPoint, out rectSize);            }            for (int i = 0; i < listCenterPoint.Count; i++)            {                //convert the center point to leftTop point of rectangle.                Point otsPoint = listCenterPoint[i];                int left = otsPoint.X - rectSize.Width / 2;                int top = otsPoint.Y - rectSize.Height / 2;//the ots coordinate is leftbottom is (0,0),but the control coordinate is leftTop is (0,0),so here use minus.                var rect = new Rectangle(new Point(left, top), rectSize);                Point lt = new Point(rect.Left, rect.Top);                Point rb = new Point(rect.Right, rect.Bottom);                RectangleF visualRect = m_visualStage.GetCtrlCoordRectF(lt, rb);                Color ColorStr = OTSSamplespaceGraphicsPanelFun.GetColorValue(ColorType.FieldColor);                string sampleName = sam.GetSampleName();                string sampleHoleName = sam.GetSampleGDIObject().HoleNo;                CVisualFieldGDIObject createRect = new CVisualFieldGDIObject(visualRect, otsPoint.X, otsPoint.Y, sampleHoleName, sampleName, ColorStr);                createRect.Enable = true;                visualfieldRects.Add(createRect);            }            return visualfieldRects;        }        public bool InitHoleImgPosData(COTSSample sample, out List<Point> lisFieldPos, out Size iSzie)        {            lisFieldPos = new List<Point>();            iSzie = new Size();            COTSSample WSample = sample;            if (null == WSample)            {                return false;            }            CDomain pMsrArea = WSample.GetMsrDomain();            COTSImgScanPrm pImgScanParam = WSample.GetMsrParams().GetImageScanParam();            COTSImageProcParam pImgProcParam = WSample.GetMsrParams().GetImageProcessParam();            CSEMDataMsr pSEMDataMsr;            List<PointF> listPoint = new List<PointF>();            listPoint.Clear();            pSEMDataMsr = new CSEMDataMsr();            pSEMDataMsr.SetScanFieldSize100(m_visualStage.GetOTSSampleStageData().iScanFieldSize100);            double dMagnification = m_MeasureAppForm.m_ProjParam.GetDefaultParam().GetStageDataParam().GetMinMag();//using the min mag to photo the hole image.            pSEMDataMsr.SetMagnification(dMagnification);            //calculate all the field position point in OTS coordination.            if (!pFieldMgr.Init(pMsrArea, pImgScanParam, 0, pSEMDataMsr, listPoint))            {                return false;            }            lisFieldPos = pFieldMgr.GetFieldCentrePoints();            var oValue = new COTSRect();            if (!pFieldMgr.GetOTSRectByIndex(0, ref oValue))            {                return false;            }            iSzie = new Size((int)oValue.GetWidth(), (int)oValue.GetHeight());            return true;        }        #endregion        public void PrepareMeasureField(CVisualSampleArea visualsam, MSR_RUN_TYPE runType)        {            try            {                List<CVisualFieldGDIObject> visualfieldRects = null;                visualfieldRects = GetOTSFieldVisualRects(visualsam, runType);//get all the possible field rects through C++ code.                if (runType == MSR_RUN_TYPE.RUNSampleHoleImage)                {                    //-------maybe take more than one visualsample hole image for one sample,                    //if the previous visual area's fields don't finish,then the unfinished field's area may overlap with the current visualsample's area.                    // then there positions will conflict. So we should remove the unfinished fields first.                    var completeHoleImgs = new List<CDisplayGDIObject>();                    foreach (var holeImgfld in m_visualStage.FrameOfHoleGDIObjects)                    {                        if (holeImgfld.BSEImage != null)                        {                            completeHoleImgs.Add(holeImgfld);                        }                    }                    m_visualStage.FrameOfHoleGDIObjects = completeHoleImgs;                    //------                    foreach (var r in visualfieldRects)                    {                        r.SetDisplayRefPoint(m_visualStage.GetDisplayRefPoint());                        r.SetZoomNumber(m_visualStage.GetZoomNum());                        RectangleF ImgRegionF;                        float ImgRegionWidth;                        float ImgRegionHeight;                        ImgRegionF = r.GetOrigionalDrawRegionF();                        ImgRegionWidth = Convert.ToInt32(ImgRegionF.Width);                        ImgRegionHeight = Convert.ToInt32(ImgRegionF.Height);                        //绘制图像                        CDisplayGDIObject createImageRect = new CDisplayGDIObject(ImgRegionF, r.OTSX, r.OTSY, r.SampleName, r.HoleNo, ImgRegionWidth, ImgRegionHeight, GDIType.DrawFrameOfHoleBSEImage);                        //添加至List集合中                        createImageRect.SetInitRegionF(r.GetOrigionalDrawRegionF());                        createImageRect.HoleNo = r.HoleNo;                        createImageRect.SetZoomNumber(m_visualStage.GetZoomNum());                        createImageRect.SetDisplayRefPoint(m_visualStage.GetDisplayRefPoint());                        m_visualStage.FrameOfHoleGDIObjects.Add(createImageRect);                    }                }                else if (runType == MSR_RUN_TYPE.RUNMEASURE)                {                    visualsam.GetMeasureGDIObject().SubItems().Clear();                    for (int i = 0; i < visualfieldRects.Count; i++)                    {                        visualsam.AddFieldGDIObject(visualfieldRects[i]);                    }                    SaveFieldPositionInfoToSample(visualsam);                    m_MeasureAppForm.UpdatePropertyVal();                }            }            catch (Exception ex)            {                log.Error(ex.ToString());            }            Invalidate();        }        public void SaveFieldPositionInfoToSample(CVisualSampleArea sam)        {            var m_ResultData = m_MeasureAppForm.m_ProjParam.GetResultData();            var sample = m_ResultData.GetSampleByName(sam.GetSampleName());            var fields = sam.GetMeasureFieldGDIObjects();            sample.ClearFields();            for (int i = 0; i < fields.Count; i++)            {                var f = (CVisualFieldGDIObject)fields[i];                var otsPoint = new Point(f.OTSX, f.OTSY);                var pixelsize = sample.CalculatePixelSize();                var newfld = new COTSField(otsPoint, pixelsize);                COTSRect r = new COTSRect();                sample.pFieldMgr.GetOTSRectByIndex(i, ref r);//get this field's global position(in the whole ots coordination)                newfld.SetOTSRect(r);                newfld.SetMeasureSequence(f.SequenceNum);                sample.pFieldMgr.CalculateFieldNeighbour(sample.GetFieldsData(), newfld);                newfld.Sample = sample;                sample.AddNewField(newfld);            }        }        #endregion        #region 设置SEM数据        public bool SetSEMDATAMParameter(double magnification, double semWorkingDistance, double bri, double contra)        {            #region 获取电镜参数            //获取Field操作类对象            if (m_SEMDATAFieldManage == null)            {                m_SEMDATAFieldManage = new SEMDATAFieldManage(m_MeasureAppForm.m_ProjParam);            }            //判断是否连接电镜            if (m_SEMDATAFieldManage.InitAndConnection())            {                //获取电镜参数                bool mFlag;                bool wFlag;                mFlag = m_SEMDATAFieldManage.SetGMagnification(magnification);                log.Warn("Set Magnification:" + magnification.ToString("F2"));                wFlag = m_SEMDATAFieldManage.SetSemWorkingDistance(semWorkingDistance);                log.Warn("Set WD:" + semWorkingDistance.ToString("F2"));                bool c = m_SEMDATAFieldManage.SetSembrightness(bri);                log.Warn("Set Brightness:" + bri.ToString("F2"));                bool d = m_SEMDATAFieldManage.SetSemContrast(contra);                log.Warn("Set Contrast:" + contra.ToString("F2"));                if (mFlag && wFlag && c && d)                {                    return true;                }            }            return false;            #endregion        }        #endregion        #region 驱动到SEM当前位置        protected void SetMeasureToSEMLocation()        {            bool selWSampleMeasureStatus = m_MeasureAppForm.m_ProjParam.GetWorkSampleMeasureStatus();            if (selWSampleMeasureStatus)            {                return;            }            try            {                List<double> SemLocation = new List<double>();                if (GetSemLocation(ref SemLocation))                {                    PointF OTSLocation = m_MeasureAppForm.m_ProjParam.ConverSEMToOTSCoord(new PointF((int)Math.Round(SemLocation[(int)XYIndex.X], 0), (int)Math.Round(SemLocation[(int)XYIndex.Y], 0)));                    NLog.LogManager.GetCurrentClassLogger().Warn(OTSLocation.ToString());                    var sam = GetWorkingVisualSample();                    var MeasureItem = sam.GetMeasureGDIObject();                    var measurePos = m_visualStage.OTSCoordToCtrlCoord(OTSLocation);                    MeasureItem.DraggingPoint = MeasureItem.GetCenterPoint();                    MeasureItem.PositionAltering(measurePos, false);                    var para = m_visualStage.GetSampleMeasurePara(MeasureItem);                    m_MeasureAppForm.m_ProjParam.SetWorkSampleHoleAndMeasureArea(para);                }                Invalidate();            }            catch (Exception ex)            {                log.Error(ex.ToString());            }        }        #endregion        #region   驱动SEM到当前位置        public bool SetSEMCurrentLocation()        {            bool bret = false;            //获取Field操作类对象            if (m_SEMDATAFieldManage == null)            {                m_SEMDATAFieldManage = new SEMDATAFieldManage(m_MeasureAppForm.m_ProjParam);            }            //判断是否连接电镜            if (m_SEMDATAFieldManage.InitAndConnection())            {                bret = m_SEMDATAFieldManage.SetSEMCurrentLocation(m_MouseDownPoint, m_visualStage);            }            return bret;        }        #endregion        #region 设置SEM到中心        public bool SetSEMToCenterPoint()        {            try            {                //获取Field操作类对象                if (m_SEMDATAFieldManage == null)                {                    m_SEMDATAFieldManage = new SEMDATAFieldManage(m_MeasureAppForm.m_ProjParam);                }                //判断是否连接电镜                bool bret = false;                var sam = GetWorkingVisualSample();                if (m_SEMDATAFieldManage.InitAndConnection())                {                    bret = (m_SEMDATAFieldManage.DriveSEMToLocation(m_MouseDownPoint, m_visualStage, sam.GetMeasureFieldGDIObjects()));                }                return bret;            }            catch (Exception ex)            {                log.Error("OTSSamplespaceWindow_" + "SetSEMToCenterPoint" + ":" + ex.ToString() + "");                return false;            }        }        #endregion        #region 获取SEM当前位置        public bool GetSemLocation(ref List<double> SemLocation)        {            //获取Field操作类对象            if (m_SEMDATAFieldManage == null)            {                m_SEMDATAFieldManage = new SEMDATAFieldManage(m_MeasureAppForm.m_ProjParam);            }            //判断是否连接电镜            bool bret = false;            if (m_SEMDATAFieldManage.InitAndConnection())            {                bret = m_SEMDATAFieldManage.GetSemLocation(ref SemLocation);            }            return bret;        }        #endregion        #region 显示标尺        /// <summary>        /// 是否显示标尺控件        /// </summary>        /// <param name="b_show"></param>        public void ShowRulerLocation()        {            //设置标尺位置            Point rulerLocation = new Point(10, this.Height - (int)(control_Ruler2.Height * 2));            this.control_Ruler2.Location = rulerLocation;            control_Ruler2.Visible = true;            var m_GlobalZoomNum = m_visualStage.GetZoomNum();            f_ruler_size = Convert.ToInt32(f_old_ruler_size * m_GlobalZoomNum);            control_Ruler2.RulerWidth = Convert.ToInt32(f_ruler_size);            control_Ruler2.Value = 1;            //放大过长时,这里将宽度与代表的实际宽度进行折半显示            while (control_Ruler2.Width > 300)            {                control_Ruler2.RulerWidth = Convert.ToInt32(control_Ruler2.RulerWidth) / 2;                control_Ruler2.Value = control_Ruler2.Value * 2;            }            control_Ruler2.SetValue(control_Ruler2.RulerWidth, control_Ruler2.Value.ToString());        }        public void RulerInit(StageDrawingData SData)        {            control_Ruler2.ShowString = "1mm";            PointF xDomain = new PointF(SData.StageDomain.Left, SData.StageDomain.Top);            PointF yDomain = new PointF(SData.StageDomain.Right, SData.StageDomain.Bottom);            //OTS宽度高度差值            float widthDomain = Math.Abs(yDomain.X - xDomain.X);            int length = this.Height > this.Width ? this.Width : this.Height;            control_Ruler2.RulerWidth = (int)(length / widthDomain * 1000);            f_ruler_size = f_old_ruler_size = control_Ruler2.RulerWidth;            control_Ruler2.Value = 1000;        }        #endregion        #region 隐藏与显示样品孔照片        static bool IsShowSampleHoleImage = true;        public void ChangeSampleHoleImage()        {            IsShowSampleHoleImage = IsShowSampleHoleImage ? false : true;            Invalidate();        }        #endregion        #region 添加样品孔中BSE图像信息        public void AddHoleBSEImageData(byte[] ImageData, int width, int height, Point fieldPos, int fieldId)        {            CDisplayGDIObject itemWorkSample;            itemWorkSample = GetWorkingVisualSample().GetSampleGDIObject();            //获取样品孔BSE图像            Image bseImage;            int m_iWidth = width;            int m_iHeigh = height;            if (m_visualStage.FrameOfHoleGDIObjects.Count > 0)            {                for (int i = 0; i < m_visualStage.FrameOfHoleGDIObjects.Count; i++)                {                    var ctrlPos = m_visualStage.OTSCoordToCtrlCoord(fieldPos);                    if (m_visualStage.FrameOfHoleGDIObjects[i].BSEImage == null)// prevent miss match when this sample area is overlaying on the other area(already took image in the field)                    {                        if (m_visualStage.FrameOfHoleGDIObjects[i].IfContains(ctrlPos))                        //if (i == fieldId)                        {                            bseImage = CImageHandler.ToGrayBitmap(ImageData, m_iWidth, m_iHeigh);                            m_visualStage.FrameOfHoleGDIObjects[i].BSEImage = bseImage;                            if (m_MeasureAppForm.MeasureThreadRunFlag)//only when it's in running mode,we update the samplehole image.prvent conflict using of sampleHoleImageBuffer.BSEImage                            {                                DrawSampleHoleImage(m_visualStage.FrameOfHoleGDIObjects[i]);                            }                            break;                        }                    }                }            }            //显示样品孔照片            IsShowSampleHoleImage = true;            Invalidate();        }        #endregion        #region 删除样品孔中BSE图像信息        public void DeleteHoleBSEImageDataNoMessageBox()        {            m_visualStage.FrameOfHoleGDIObjects.Clear();            var w = sampleHoleImageBuffer.BSEImage.Width;            var h = sampleHoleImageBuffer.BSEImage.Height;            sampleHoleImageBuffer.BSEImage = new Bitmap(w, h);            Invalidate();        }        #endregion        #region 复位工作样品区域        public CDisplayGDIObject ResetMeasure(CDisplayGDIObject SampleGDIObjects, CMeasureArea MeasureGDIObjects)        {            m_visualStage.GetMeasureGdiObjectFromSampleGdi(SampleGDIObjects, out MeasureGDIObjects);            return MeasureGDIObjects;        }        public void InverseSelectWorkSampleMeasure()        {            m_DrawMeasureType = DrawMeasureAreaActionType.InverseSelection;            m_IsDrawMeasure = true;            m_DrawMeasureGDIObject = null;            this.Cursor = Cursors.Hand;        }        #endregion        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)        {            this.OnMouseDown(e);        }        private void pictureBox1_Paint(object sender, PaintEventArgs e)        {            OnPaint(e, GetAllVisualFieldGdiObject());            if (m_DrawMeasureGDIObject != null)            {                m_DrawMeasureGDIObject.OnPaint(e);            }            if (m_DrawPolygonFinishGDIObject != null) m_DrawPolygonFinishGDIObject.OnPaint(e);        }        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)        {            this.OnMouseUp(e);        }        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)        {            this.OnMouseMove(e);        }          }}
 |