DocumentView.cs 193 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346
  1. using PaintDotNet.Adjust;
  2. using PaintDotNet.Adjust.BaseImage;
  3. using PaintDotNet.Annotation;
  4. using PaintDotNet.Annotation.Command;
  5. using PaintDotNet.Annotation.DedicatedAnalysis;
  6. using PaintDotNet.Annotation.Enum;
  7. using PaintDotNet.Annotation.FieldView;
  8. using PaintDotNet.Annotation.ImageCollect;
  9. using PaintDotNet.Annotation.Label;
  10. using PaintDotNet.Annotation.Measure;
  11. using PaintDotNet.Annotation.Other;
  12. using PaintDotNet.Annotation.PhysicalPhaseAction;
  13. using PaintDotNet.Base;
  14. using PaintDotNet.Base.CommTool;
  15. using PaintDotNet.Base.DedicatedAnalysis.Inclusions.Model;
  16. using PaintDotNet.Base.Enum;
  17. using PaintDotNet.Base.Functionodel;
  18. using PaintDotNet.Base.SettingModel;
  19. using PaintDotNet.Base.SettingModel.LVMModel;
  20. using PaintDotNet.Base.XmlSaveModel;
  21. using PaintDotNet.DbOpreate.DbModel;
  22. using PaintDotNet.SystemLayer;
  23. using System;
  24. using System.Collections.Generic;
  25. using System.ComponentModel;
  26. using System.Drawing;
  27. using System.Drawing.Drawing2D;
  28. using System.Reflection;
  29. using System.Runtime.InteropServices;
  30. using System.Threading;
  31. using System.Windows.Forms;
  32. using static PaintDotNet.Base.SettingModel.LabelStyleModel;
  33. namespace PaintDotNet
  34. {
  35. /// <summary>
  36. /// 主面板
  37. /// </summary>
  38. public class DocumentView : UserControl2, ISurfaceBox, DocumentDirtyObserver
  39. {
  40. #region 控件
  41. private IContainer components = null;
  42. /// <summary>
  43. /// 左侧刻度标尺
  44. /// </summary>
  45. public Ruler leftRuler;
  46. /// <summary>
  47. /// 中心画布
  48. /// </summary>
  49. public PanelEx panel;
  50. /// <summary>
  51. /// 顶部刻度标尺
  52. /// </summary>
  53. public Ruler topRuler;
  54. /// <summary>
  55. /// 右键菜单
  56. /// </summary>
  57. protected ContextMenuStrip contextMenuStrip1;
  58. protected ToolStripMenuItem toolStripMenuItem1;
  59. protected ToolStripMenuItem toolStripMenuItem2;
  60. protected ToolStripMenuItem toolStripMenuItem3;
  61. protected ToolStripMenuItem toolStripMenuItem4;
  62. #endregion
  63. /// <summary>
  64. /// 是否创建缩略图
  65. /// </summary>
  66. public bool IsCreatThumb = true;
  67. private bool raiseFirstInputAfterGotFocus = false;
  68. private bool hookedMouseEvents = false;
  69. public static WeakReference<Surface> doubleBufferSurfaceWeakRef = null;
  70. public Surface doubleBufferSurface = null;
  71. //private static WeakReference<Surface> doubleBufferSurfaceWeakRef1 = null;
  72. //private Surface doubleBufferSurface1 = null;
  73. /// <summary>
  74. /// 清理DoubleBuffer
  75. /// </summary>
  76. public void ClearDoubleBuffer()
  77. {
  78. doubleBufferSurfaceWeakRef = null;
  79. doubleBufferSurface = null;
  80. }
  81. /// <summary>
  82. /// 绘制线程
  83. /// </summary>
  84. private Threading.ThreadPool threadPool = new Threading.ThreadPool();
  85. private RenderContext renderContext, renderContext_Phase;
  86. /// <summary>
  87. /// 为了保证图片绘制在画布中心
  88. /// 设置的偏移量
  89. /// </summary>
  90. private int offsetW = 0;
  91. private int offsetH = 0;
  92. private int offsetHalfW = 0;
  93. private int offsetHalfH = 0;
  94. /*private int offsetW = 300;
  95. private int offsetH = 150;
  96. private int offsetHalfW = 150;
  97. private int offsetHalfH = 75;*/
  98. public TreeView oldDrawTreeView;
  99. /// <summary>
  100. /// 相的集合
  101. /// </summary>
  102. public List<PhaseModel> phaseModels = new List<PhaseModel>();
  103. /// <summary>
  104. /// 满屏网格是否显示,默认不显示
  105. /// </summary>
  106. private bool gridLineFullEnabled = false;
  107. /// <summary>
  108. /// 网格是否显示,默认不显示
  109. /// </summary>
  110. private bool gridLineEnabled = false;
  111. /// <summary>
  112. /// 矩形网格是否显示,默认不显示
  113. /// </summary>
  114. private bool gridRectangleEnabled = false;
  115. /// <summary>
  116. /// 圆形网格是否显示,默认不显示
  117. /// </summary>
  118. private bool gridRoundEnabled = false;
  119. /// <summary>
  120. /// 十字线是否显示,默认不显示
  121. /// </summary>
  122. private bool gridCrossCurveEnabled = false;
  123. /// <summary>
  124. /// 辅助线是否显示,默认不显示
  125. /// </summary>
  126. private bool auxiliaryLineEnabled = false;
  127. /// <summary>
  128. /// 视图标尺是否显示,默认显示
  129. /// </summary>
  130. private bool rulersEnabled = false;
  131. /// <summary>
  132. /// 当前的视场行为标记,合并或删除
  133. /// </summary>
  134. private CombineMode combineMode = CombineMode.Union;
  135. /// <summary>
  136. /// 测量连续绘制
  137. /// </summary>
  138. public static bool ContinuousDrawing = false;
  139. /// <summary>
  140. /// 二值提取\连续操作
  141. /// </summary>
  142. private bool continuousBinaryAction = false;
  143. /// <summary>
  144. /// 实际大小标记
  145. /// </summary>
  146. private bool actualsize = false;
  147. /// <summary>
  148. /// 合适大小标记
  149. /// </summary>
  150. private bool suitableSize = false;
  151. /// <summary>
  152. /// 合适高度标记
  153. /// </summary>
  154. private bool suitableHeight = false;
  155. /// <summary>
  156. /// 合适宽度标记
  157. /// </summary>
  158. private bool suitableWidth = false;
  159. /// <summary>
  160. /// 锁定扩缩
  161. /// </summary>
  162. private bool lockZoom = false;
  163. /// <summary>
  164. /// 定倍显示
  165. /// </summary>
  166. private bool fixedMultiple = false;
  167. /// <summary>
  168. /// 合并视场
  169. /// </summary>
  170. private bool mergeFieldOfView = false;
  171. /// <summary>
  172. /// 删除视场
  173. /// </summary>
  174. private bool deleteFieldOfView = false;
  175. /// <summary>
  176. /// 图层及文档信息
  177. /// </summary>
  178. private Document document;
  179. /// <summary>
  180. /// 图像内存块及其属性
  181. /// </summary>
  182. protected Surface compositionSurface;
  183. /// <summary>
  184. /// 【标注、测量】用于保存测量、标注等的历史记录,用于撤销
  185. /// </summary>
  186. public UndoManager undoManager;
  187. /// <summary>
  188. /// 【标注、测量】list of draw objects
  189. /// </summary>
  190. private GraphicsList graphicsList;
  191. /// <summary>
  192. /// 【标注、测量、视场、其它】当前激活的工具
  193. /// </summary>
  194. public DrawToolType _activeTool;
  195. /// <summary>
  196. /// 工具的集合
  197. /// </summary>
  198. protected static Dictionary<DrawToolType, Type> tools;
  199. /// <summary>
  200. /// 初始化标记
  201. /// </summary>
  202. private bool initialized;
  203. /// <summary>
  204. /// AppWorkspace接口
  205. /// </summary>
  206. private IAppWorkspaceForSurfaceBox appWorkspace;
  207. /// <summary>
  208. /// 目前用于修改视场,如果是鼠标保持按下的状态,比如按下不抬起状态,
  209. /// 按下拖动状态,则不响应修改视场界面的相应numericUpDown等控件的值变化事件,
  210. /// 以避免事件的冲突
  211. /// </summary>
  212. public bool mouseStatus;
  213. /// <summary>
  214. /// 辅助线的横线和竖线
  215. /// </summary>
  216. GraphicsPath path, path1;
  217. /// <summary>
  218. /// 待渲染的列表
  219. /// </summary>
  220. private SurfaceBoxRendererList rendererList;
  221. private SurfaceBoxBaseRenderer baseRenderer;
  222. /// <summary>
  223. /// 窗体状态
  224. /// </summary>
  225. private FormWindowState oldWindowState = FormWindowState.Minimized;
  226. /// <summary>
  227. /// 缩放比例
  228. /// </summary>
  229. private ScaleFactor scaleFactor = new ScaleFactor(1, 1);
  230. /// <summary>
  231. /// 每像素多少微米
  232. /// </summary>
  233. private double micronRatio;
  234. /// <summary>
  235. /// 是否允许像素跟踪
  236. /// </summary>
  237. public bool pixelTrackingEnabled = true;
  238. /// <summary>
  239. /// 是否允许刷新公共的底部的放大缩小的进度条和值
  240. /// </summary>
  241. public bool refueshZoomTrackValue = true;
  242. protected int toolNumber = -1;
  243. public DrawAnalysisModel AnalysisStyleModel { get; set; }
  244. /// <summary>
  245. /// 如果是false,则是弹窗的DocumentWorkspaceWindow
  246. /// </summary>
  247. public bool rightDown = true;
  248. /// <summary>
  249. /// ctrl按下事件
  250. /// </summary>
  251. public bool controlPress = false;
  252. /// <summary>
  253. /// 在toolPointer的工具下,是否绘制鼠标划选的矩形
  254. /// </summary>
  255. public bool drawRectangle = false;
  256. /// <summary>
  257. /// 在toolPointer的工具下,鼠标划选的矩形
  258. /// </summary>
  259. public Rectangle rectangle;
  260. public Inclusion inclusion;
  261. #region 直方图操作数据缓存
  262. /// <summary>
  263. /// 亮度
  264. /// </summary>
  265. private double histogramBeta = -0.5;
  266. /// <summary>
  267. /// 对比度
  268. /// </summary>
  269. private double histogramAlpha = 1.0;
  270. /// <summary>
  271. /// gamma值
  272. /// </summary>
  273. private double histogramGamma = 1.0;
  274. /// <summary>
  275. /// 最佳算法的数值,范围0-499
  276. /// </summary>
  277. private int histogramPercent = 200;
  278. /// <summary>
  279. /// 是否选中了log
  280. /// </summary>
  281. private bool histogramLogEnabled;
  282. /// <summary>
  283. /// 是否选中了skip
  284. /// </summary>
  285. private bool histogramSkipEnabled;
  286. #endregion
  287. #region 直方图操作数据缓存
  288. /// <summary>
  289. /// 亮度
  290. /// </summary>
  291. public double HistogramBeta
  292. {
  293. set
  294. {
  295. this.histogramBeta = value;
  296. }
  297. get
  298. {
  299. return this.histogramBeta;
  300. }
  301. }
  302. /// <summary>
  303. /// 对比度
  304. /// </summary>
  305. public double HistogramAlpha
  306. {
  307. set
  308. {
  309. this.histogramAlpha = value;
  310. }
  311. get
  312. {
  313. return this.histogramAlpha;
  314. }
  315. }
  316. public double HistogramGamma
  317. {
  318. set
  319. {
  320. this.histogramGamma = value;
  321. }
  322. get
  323. {
  324. return this.histogramGamma;
  325. }
  326. }
  327. public int HistogramPercent
  328. {
  329. set
  330. {
  331. this.histogramPercent = value;
  332. }
  333. get
  334. {
  335. return this.histogramPercent;
  336. }
  337. }
  338. public bool HistogramLogEnabled
  339. {
  340. set
  341. {
  342. this.histogramLogEnabled = value;
  343. }
  344. get
  345. {
  346. return this.histogramLogEnabled;
  347. }
  348. }
  349. public bool HistogramSkipEnabled
  350. {
  351. set
  352. {
  353. this.histogramSkipEnabled = value;
  354. }
  355. get
  356. {
  357. return this.histogramSkipEnabled;
  358. }
  359. }
  360. #endregion
  361. public DocumentView()
  362. {
  363. InitializeComponent();
  364. this.document = null;
  365. this.compositionSurface = null;
  366. this.panel.ScaleFactor = this.scaleFactor;
  367. this.panel.Paint += new PaintEventHandler(this.panelPaint);
  368. this.panel.MouseMove += new MouseEventHandler(this.MouseEvent_Move);
  369. this.panel.MouseDown += new MouseEventHandler(this.MouseEvent_Down);
  370. this.panel.MouseUp += new MouseEventHandler(this.MouseEvent_Up);
  371. this.panel.MouseClick += new MouseEventHandler(this.MouseEvent_Click);
  372. this.panel.MouseDoubleClick += new MouseEventHandler(this.MouseEvent_DoubleClick);
  373. this.rendererList = new SurfaceBoxRendererList(this.panel.Size, this.panel.Size);
  374. this.rendererList.Invalidated += new InvalidateEventHandler(Renderers_Invalidated);
  375. this.baseRenderer = new SurfaceBoxBaseRenderer(this.rendererList, null);
  376. this.rendererList.Add(this.baseRenderer, false);
  377. //phaseModels.ListChanged += new ListChangedEventHandler<PhaseModel>(this.phaseModels_Change);
  378. this.initialized = true;
  379. }
  380. public unsafe void phaseModels_Change(object sender)
  381. {
  382. this.rendererList.topList = new SurfaceBoxRenderer[0];
  383. foreach (PhaseModel model in phaseModels)
  384. {
  385. Surface surface1 = null;
  386. if (model.mat != null)
  387. {
  388. surface1 = new Surface(model.mat.Width, model.mat.Height);
  389. surface1.mat = model.mat.Clone();
  390. surface1.scan0.VoidStar = (void*)model.mat.Data;
  391. surface1.Stride = (int)model.mat.Step();
  392. }
  393. this.rendererList.Add(new SurfaceBoxBaseRenderer(this.rendererList, surface1), true);
  394. }
  395. }
  396. public new void Refresh()
  397. {
  398. this.phaseModels_Change(null);
  399. base.Refresh();
  400. }
  401. public OpenCvSharp.Mat OldMat
  402. {
  403. get
  404. {
  405. if (this.CompositionSurface != null && CompositionSurface != null && CompositionSurface.mat != null)
  406. return this.CompositionSurface.BackUpMat;
  407. else
  408. return null;
  409. }
  410. }
  411. public OpenCvSharp.Mat BoxMat
  412. {
  413. get
  414. {
  415. if(this.CompositionSurface.Width>10000 || this.CompositionSurface.Height>10000)
  416. {
  417. OpenCvSharp.Mat temp = new OpenCvSharp.Mat();
  418. int width = this.CompositionSurface.Width / 10;
  419. int height = this.CompositionSurface.Height / 10;
  420. OpenCvSharp.Cv2.Resize(this.CompositionSurface.BackUpMat, temp, new OpenCvSharp.Size(width==0 ? 1 : width, height==0?1:height));
  421. return temp;
  422. }
  423. else
  424. {
  425. return this.CompositionSurface.BackUpMat;
  426. }
  427. }
  428. }
  429. public Rectangle DrawRectangle
  430. {
  431. get
  432. {
  433. return this.rectangle;
  434. }
  435. set
  436. {
  437. this.rectangle = value;
  438. }
  439. }
  440. public bool DrawRectangleFlag
  441. {
  442. get
  443. {
  444. return this.drawRectangle;
  445. }
  446. set
  447. {
  448. this.drawRectangle = value;
  449. }
  450. }
  451. public IAppWorkspaceForSurfaceBox AppWorkspaceTop
  452. {
  453. set
  454. {
  455. this.appWorkspace = value;
  456. }
  457. get
  458. {
  459. return this.appWorkspace;
  460. }
  461. }
  462. public GraphicsList GraphicsList
  463. {
  464. get
  465. {
  466. return graphicsList;
  467. }
  468. set
  469. {
  470. graphicsList = value;
  471. if (this.GraphicsList != null)
  472. {
  473. this.AdjustRendering();
  474. //this.graphicsList.AddObserver(this);
  475. }
  476. }
  477. }
  478. void AdjustRendering()
  479. {
  480. Size docSize;
  481. if (this.GraphicsList != null)
  482. {
  483. docSize = this.GraphicsList.GetSize();
  484. //AutoScrollMinSize = docSize;
  485. }
  486. else
  487. {
  488. //AutoScrollMinSize = new Size(0, 0);
  489. }
  490. Invalidate();
  491. }
  492. public Surface CompositionSurface
  493. {
  494. get
  495. {
  496. return this.compositionSurface;
  497. }
  498. set
  499. {
  500. if (value != null)
  501. {
  502. this.compositionSurface = value;
  503. this.Refresh();
  504. }
  505. }
  506. }
  507. public bool GridLineEnabled
  508. {
  509. get
  510. {
  511. return this.gridLineEnabled;
  512. }
  513. set
  514. {
  515. this.gridLineEnabled = value;
  516. }
  517. }
  518. public bool GridLineFullEnabled
  519. {
  520. get
  521. {
  522. return this.gridLineFullEnabled;
  523. }
  524. set
  525. {
  526. this.gridLineFullEnabled = value;
  527. }
  528. }
  529. public bool GridRectangleEnabled
  530. {
  531. get
  532. {
  533. return this.gridRectangleEnabled;
  534. }
  535. set
  536. {
  537. this.gridRectangleEnabled = value;
  538. }
  539. }
  540. public bool GridRoundEnabled
  541. {
  542. get
  543. {
  544. return this.gridRoundEnabled;
  545. }
  546. set
  547. {
  548. this.gridRoundEnabled = value;
  549. }
  550. }
  551. public bool GridCrossCurveEnabled
  552. {
  553. get
  554. {
  555. return this.gridCrossCurveEnabled;
  556. }
  557. set
  558. {
  559. this.gridCrossCurveEnabled = value;
  560. }
  561. }
  562. public double MicronRatio
  563. {
  564. get
  565. {
  566. return this.micronRatio;
  567. }
  568. set
  569. {
  570. this.micronRatio = value;
  571. }
  572. }
  573. public bool AuxiliaryLineEnabled
  574. {
  575. get
  576. {
  577. return auxiliaryLineEnabled;
  578. }
  579. set
  580. {
  581. this.auxiliaryLineEnabled = value;
  582. }
  583. }
  584. public bool ContinuousDrawingLabel
  585. {
  586. get
  587. {
  588. return ContinuousDrawing;
  589. }
  590. set
  591. {
  592. ContinuousDrawing = value;
  593. }
  594. }
  595. public bool ContinuousDrawingMeasure
  596. {
  597. get
  598. {
  599. return ContinuousDrawing;
  600. }
  601. set
  602. {
  603. ContinuousDrawing = value;
  604. }
  605. }
  606. public bool ContinuousBinaryAction
  607. {
  608. get
  609. {
  610. return continuousBinaryAction;
  611. }
  612. set
  613. {
  614. this.continuousBinaryAction = value;
  615. }
  616. }
  617. public bool ActualSize
  618. {
  619. get
  620. {
  621. return actualsize;
  622. }
  623. set
  624. {
  625. this.actualsize = value;
  626. }
  627. }
  628. public bool SuitableSize
  629. {
  630. get
  631. {
  632. return suitableSize;
  633. }
  634. set
  635. {
  636. this.suitableSize = value;
  637. }
  638. }
  639. public bool SuitableWidth
  640. {
  641. get
  642. {
  643. return suitableWidth;
  644. }
  645. set
  646. {
  647. this.suitableWidth = value;
  648. }
  649. }
  650. public bool SuitableHeight
  651. {
  652. get
  653. {
  654. return suitableHeight;
  655. }
  656. set
  657. {
  658. this.suitableHeight = value;
  659. }
  660. }
  661. public bool LockZoom
  662. {
  663. get
  664. {
  665. return lockZoom;
  666. }
  667. set
  668. {
  669. this.lockZoom = value;
  670. }
  671. }
  672. public bool FixedMultiple
  673. {
  674. get
  675. {
  676. return fixedMultiple;
  677. }
  678. set
  679. {
  680. this.fixedMultiple = value;
  681. }
  682. }
  683. public bool MergeFieldOfView
  684. {
  685. get
  686. {
  687. return mergeFieldOfView;
  688. }
  689. set
  690. {
  691. this.mergeFieldOfView = value;
  692. }
  693. }
  694. public bool DeleteFieldOfView
  695. {
  696. get
  697. {
  698. return deleteFieldOfView;
  699. }
  700. set
  701. {
  702. this.deleteFieldOfView = value;
  703. }
  704. }
  705. public bool RulersEnabled
  706. {
  707. get
  708. {
  709. return rulersEnabled;
  710. }
  711. set
  712. {
  713. //if (this.rulersEnabled != value)
  714. {
  715. this.rulersEnabled = value;
  716. if (this.topRuler != null)
  717. {
  718. this.topRuler.Enabled = value;
  719. this.topRuler.Visible = value;
  720. }
  721. if (this.leftRuler != null)
  722. {
  723. this.leftRuler.Enabled = value;
  724. this.leftRuler.Visible = value;
  725. }
  726. this.OnResize(EventArgs.Empty);
  727. OnRulersEnabledChanged();
  728. }
  729. }
  730. }
  731. public CombineMode CombineMode
  732. {
  733. get
  734. {
  735. return this.combineMode;
  736. }
  737. set
  738. {
  739. this.combineMode = value;
  740. }
  741. }
  742. public SurfaceBoxRendererList RendererList
  743. {
  744. get
  745. {
  746. return this.rendererList;
  747. }
  748. }
  749. /// <summary>
  750. /// 初始化工具
  751. /// </summary>
  752. protected void InitToolsAndManager()
  753. {
  754. // 一是用于存储,二是用于调整层级
  755. GraphicsList = new GraphicsList(this);
  756. // 创建管理器,用于前进后退
  757. undoManager = new UndoManager(GraphicsList);
  758. // init Tools
  759. //tools = new Tool[(int)DrawToolType.NumberOfDrawTools];
  760. if (tools != null) return;
  761. tools = new Dictionary<DrawToolType, Type>();
  762. tools.Add(DrawToolType.Pointer, typeof(ToolPointer));
  763. //
  764. //标注
  765. //
  766. tools.Add(DrawToolType.DrawRectangle, typeof(ToolRectangle));
  767. tools.Add(DrawToolType.DrawEllipse, typeof(ToolEllipse));
  768. tools.Add(DrawToolType.DrawLine, typeof(ToolLine));
  769. tools.Add(DrawToolType.DrawPolygon, typeof(ToolPolygon));
  770. tools.Add(DrawToolType.DrawPolygonLine, typeof(ToolPolygonLine));
  771. tools.Add(DrawToolType.DrawPencil, typeof(ToolPencil));
  772. tools.Add(DrawToolType.DrawClosedCurve, typeof(ToolClosedCurve));
  773. tools.Add(DrawToolType.DrawCurve, typeof(ToolCurve));
  774. tools.Add(DrawToolType.DrawCircle, typeof(ToolCircle));
  775. tools.Add(DrawToolType.DrawOneArrowLine, typeof(ToolOneArrowLine));
  776. tools.Add(DrawToolType.DrawTwoArrowLine, typeof(ToolTwoArrowLine));
  777. tools.Add(DrawToolType.DrawLineSegment, typeof(ToolLineSegment));
  778. tools.Add(DrawToolType.DrawRoundRectangle, typeof(ToolRoundRectangle));
  779. tools.Add(DrawToolType.DrawTextString, typeof(ToolTextString));
  780. tools.Add(DrawToolType.DrawDateMark, typeof(ToolDateMark));
  781. tools.Add(DrawToolType.DrawTimeMark, typeof(ToolTimeMark));
  782. tools.Add(DrawToolType.DrawNumberMark, typeof(ToolNumberMark));
  783. tools.Add(DrawToolType.DrawGainNumber, typeof(ToolGainNumber));
  784. tools.Add(DrawToolType.DrawWorkType, typeof(ToolWorkType));
  785. tools.Add(DrawToolType.DrawPointMark, typeof(ToolPointMark));
  786. tools.Add(DrawToolType.DrawWaterMark, typeof(ToolWaterMark));
  787. //分析绘图
  788. tools.Add(DrawToolType.DrawCircleA, typeof(ToolCircleA));
  789. tools.Add(DrawToolType.DrawMulLineA, typeof(ToolDrawMulLineA));
  790. //
  791. //测量
  792. //
  793. tools.Add(DrawToolType.MeasureLine, typeof(ToolMeasureLine));
  794. tools.Add(DrawToolType.MeasureDistanceLine, typeof(ToolMeasureDistanceLine));
  795. tools.Add(DrawToolType.MeasureLength, typeof(ToolMeasureLength));
  796. tools.Add(DrawToolType.MeasureTraceCurve, typeof(ToolMeasureTraceCurve));
  797. tools.Add(DrawToolType.MeasureHLine, typeof(ToolMeasureHLine));
  798. tools.Add(DrawToolType.MeasureVLine, typeof(ToolMeasureVLine));
  799. tools.Add(DrawToolType.MeasureCircle, typeof(ToolMeasureCircle));
  800. tools.Add(DrawToolType.MeasureInnerCircle, typeof(ToolMeasureInnerCircle));
  801. tools.Add(DrawToolType.MeasureOuterCircle, typeof(ToolMeasureOuterCircle));
  802. tools.Add(DrawToolType.MeasureDiameterCircle, typeof(ToolMeasureDiameterCircle));
  803. tools.Add(DrawToolType.MeasurePointEdgeSize, typeof(ToolMeasurePointEdgeSize));
  804. tools.Add(DrawToolType.MeasurePointCenterSize, typeof(ToolMeasurePointCenterSize));
  805. tools.Add(DrawToolType.MeasureBrokenLine, typeof(ToolMeasureBrokenLine));
  806. tools.Add(DrawToolType.MeasureCurveLine, typeof(ToolMeasureCurveLine));
  807. tools.Add(DrawToolType.MeasureThreePointAngle, typeof(ToolMeasureThreePointAngle));
  808. tools.Add(DrawToolType.MeasureFourPointAngle, typeof(ToolMeasureFourPointAngle));
  809. tools.Add(DrawToolType.MeasureThreePointArc, typeof(ToolMeasureThreePointArc));
  810. tools.Add(DrawToolType.MeasurePLine, typeof(ToolMeasurePLine));
  811. tools.Add(DrawToolType.MeasureMulPLine, typeof(ToolMeasureMulPLine));
  812. tools.Add(DrawToolType.MeasureHMulPLine, typeof(ToolMeasureHMulPLine));
  813. tools.Add(DrawToolType.MeasureVMulPLine, typeof(ToolMeasureVMulPLine));
  814. tools.Add(DrawToolType.MeasureParallelLine, typeof(ToolMeasureParallelLine));
  815. tools.Add(DrawToolType.MeasureMulParallelLine, typeof(ToolMeasureMulParallelLine));
  816. tools.Add(DrawToolType.MeasureVMulParallelLine, typeof(ToolMeasureVMulParallelLine));
  817. tools.Add(DrawToolType.MeasureHMulParallelLine, typeof(ToolMeasureHMulParallelLine));
  818. tools.Add(DrawToolType.MeasureClosedCurve, typeof(ToolMeasureClosedCurve));
  819. tools.Add(DrawToolType.MeasurePolygon, typeof(ToolMeasurePolygon));
  820. tools.Add(DrawToolType.MeasureRectangle, typeof(ToolMeasureRectangle));
  821. tools.Add(DrawToolType.MeasureRandRectangle, typeof(ToolMeasureRandRectangle));
  822. tools.Add(DrawToolType.MeasureSquare, typeof(ToolMeasureSquare));
  823. tools.Add(DrawToolType.MeasureTracePolygon, typeof(ToolMeasureTracePolygon));
  824. tools.Add(DrawToolType.MeasureMulLine, typeof(ToolMeasureMulLine));
  825. tools.Add(DrawToolType.MeasureMulSegment, typeof(ToolMeasureMulSegment));
  826. tools.Add(DrawToolType.MeasureMulHVLine, typeof(ToolMeasureMulHVLine));
  827. tools.Add(DrawToolType.MeasureMulVLine, typeof(ToolMeasureMulVLine));
  828. tools.Add(DrawToolType.MeasureRandSquare, typeof(ToolMeasureRandSquare));
  829. tools.Add(DrawToolType.MeasurePointHLine, typeof(ToolMeasurePointHLine));
  830. tools.Add(DrawToolType.MeasurePointArcSize, typeof(ToolMeasurePointArcSize));
  831. tools.Add(DrawToolType.MeasureCenterCenterSize, typeof(ToolMeasureCenterCenterSize));
  832. tools.Add(DrawToolType.MeasureTwoLineVLDistance, typeof(ToolMeasureTwoLineVLDistance));
  833. //
  834. //视场
  835. //
  836. tools.Add(DrawToolType.ViewOval, typeof(ToolViewOval));
  837. tools.Add(DrawToolType.ViewCircle, typeof(ToolViewCircle));
  838. tools.Add(DrawToolType.ViewRectangle, typeof(ToolViewRectangle));
  839. tools.Add(DrawToolType.ViewRectangleEx, typeof(ToolViewRectangleEx));
  840. tools.Add(DrawToolType.ViewTriangle, typeof(ToolViewTriangle));
  841. tools.Add(DrawToolType.ViewTriangleEx, typeof(ToolViewTriangleEx));
  842. tools.Add(DrawToolType.ViewSquare, typeof(ToolViewSquare));
  843. tools.Add(DrawToolType.ViewPolygon, typeof(ToolViewPolygon));
  844. #region 对象处理
  845. //单个提取
  846. tools.Add(DrawToolType.BinaryExtract, typeof(ToolBinaryExtract));
  847. //选择
  848. tools.Add(DrawToolType.BinaryChoise, typeof(ToolBinaryChoise));
  849. tools.Add(DrawToolType.BinaryChoiseRectangle, typeof(ToolBinaryChoiseRectangle));
  850. tools.Add(DrawToolType.BinaryChoiseOval, typeof(ToolBinaryChoiseOval));
  851. tools.Add(DrawToolType.BinaryChoisePolygon, typeof(ToolBinaryChoisePolygon));
  852. //添加
  853. tools.Add(DrawToolType.BinaryAddOval, typeof(ToolBinaryAddOval));
  854. tools.Add(DrawToolType.BinaryAddRectangle, typeof(ToolBinaryAddRectangle));
  855. tools.Add(DrawToolType.BinaryAddPolygon, typeof(ToolBinaryAddPolygon));
  856. tools.Add(DrawToolType.BinaryAddTrack, typeof(ToolBinaryAddTrack));
  857. //删除
  858. tools.Add(DrawToolType.BinaryDelete, typeof(ToolBinaryDelete));
  859. tools.Add(DrawToolType.BinaryDeleteRectangle, typeof(ToolBinaryDeleteRectangle));
  860. tools.Add(DrawToolType.BinaryDeletePolygon, typeof(ToolBinaryDeletePolygon));
  861. tools.Add(DrawToolType.BinaryDeleteOval, typeof(ToolBinaryDeleteOval));
  862. //连接
  863. tools.Add(DrawToolType.BinaryConnectionLine, typeof(ToolBinaryConnectionLine));
  864. tools.Add(DrawToolType.BinaryConnectionPolygonLine, typeof(ToolBinaryConnectionPolygonLine));
  865. tools.Add(DrawToolType.BinaryConnectionOval, typeof(ToolBinaryConnectionOval));
  866. //分割
  867. tools.Add(DrawToolType.BinarySplitLine, typeof(ToolBinarySplitLine));
  868. tools.Add(DrawToolType.BinarySplitPolyline, typeof(ToolBinarySplitPolyLine));
  869. tools.Add(DrawToolType.BinarySplitOval, typeof(ToolBinarySplitOval));
  870. #endregion
  871. #region 专用分析
  872. // 夹杂物
  873. tools.Add(DrawToolType.InclusionNoEffect, typeof(ToolInclusionNoEffect));
  874. tools.Add(DrawToolType.InclusionSelect, typeof(ToolInclusionSelect));
  875. tools.Add(DrawToolType.InclusionPolygon, typeof(ToolInclusionPolygon));
  876. tools.Add(DrawToolType.InclusionNewPolygon, typeof(ToolInclusionNewPolygon));
  877. tools.Add(DrawToolType.InclusionDrawRecognitionArea, typeof(ToolInclusionDrawRecognitionArea));
  878. tools.Add(DrawToolType.InclusionSelectRecognitionArea, typeof(ToolInclusionSelectRecognitionArea));
  879. #endregion
  880. #region 物相提取
  881. //多边形
  882. tools.Add(DrawToolType.PPhasePolygon, typeof(ToolPPhasePolygon));
  883. //矩形
  884. tools.Add(DrawToolType.PPhaseRectangle, typeof(ToolPPhaseRectangle));
  885. //椭圆
  886. tools.Add(DrawToolType.PPhaseOval, typeof(ToolPPhaseOval));
  887. #endregion
  888. //其它
  889. //手型工具
  890. tools.Add(DrawToolType.MoveMode, typeof(PanTool));
  891. //图片裁剪
  892. tools.Add(DrawToolType.ImageCut, typeof(ImageCutTool));
  893. //自动标尺
  894. tools.Add(DrawToolType.DrawAutoRuler, typeof(ToolAutoRuler));
  895. //预存标尺
  896. tools.Add(DrawToolType.DrawPrestoredRuler, typeof(ToolPrestoredRuler));
  897. //手动标尺
  898. tools.Add(DrawToolType.DrawHandModeRuler, typeof(ToolHandModeRuler));
  899. //光密度直线绘制
  900. tools.Add(DrawToolType.OpticalDensityLine, typeof(ToolOpticalDensityLine));
  901. //划痕处理
  902. tools.Add(DrawToolType.DrawScratchTreatmentLine, typeof(ToolScratchTreatmentLine));
  903. //污迹处理-矩形
  904. tools.Add(DrawToolType.DrawSmudgeRectangle, typeof(ToolSmudgeRectangle));
  905. //污迹处理-多边形
  906. tools.Add(DrawToolType.DrawSmudgePolygon, typeof(ToolSmudgePolygon));
  907. //污迹处理-圆形
  908. tools.Add(DrawToolType.DrawSmudgeCircle, typeof(ToolSmudgeCircle));
  909. //污迹处理-椭圆
  910. tools.Add(DrawToolType.DrawSmudgeEllipse, typeof(ToolSmudgeEllipse));
  911. //吸管工具
  912. tools.Add(DrawToolType.ColorPicker, typeof(ColorPickerTool));
  913. //null
  914. tools.Add(DrawToolType.NullTool, typeof(ToolNull));
  915. // 图像拼接-矩形
  916. tools.Add(DrawToolType.DrawStitchingRectangle, typeof(ToolStitchingRectangle));
  917. // 图像拼接-圆形
  918. tools.Add(DrawToolType.DrawStitchingCircle, typeof(ToolStitchingCircle));
  919. // 图像拼接-多边形
  920. tools.Add(DrawToolType.DrawStitchingPolygon, typeof(ToolStitchingPolygon));
  921. // 工艺图对照点矩形
  922. tools.Add(DrawToolType.DrawArtworkRectangle, typeof(ToolArtworkRectangle));
  923. // 位置列表十字线
  924. tools.Add(DrawToolType.DrawLocationCross, typeof(ToolLocationCross));
  925. }
  926. public MeasurementUnit Units
  927. {
  928. get
  929. {
  930. return this.leftRuler.MeasurementUnit;
  931. }
  932. set
  933. {
  934. OnUnitsChanging();
  935. this.leftRuler.MeasurementUnit = value;
  936. this.topRuler.MeasurementUnit = value;
  937. DocumentMetaDataChangedHandler(this, EventArgs.Empty);
  938. OnUnitsChanged();
  939. }
  940. }
  941. protected virtual void OnUnitsChanging()
  942. {
  943. }
  944. protected virtual void OnUnitsChanged()
  945. {
  946. }
  947. /// <summary>
  948. /// 获取系统当前选中单位及每单位像素值
  949. /// </summary>
  950. /// <returns>3位字符串数组,
  951. /// 0:系统选中单位枚举字符串
  952. /// 1:系统选中单位名称字符串
  953. /// 2:系统选中单位符号字符串
  954. /// 3:系统选中单位每单位像素长度
  955. /// 4:系统选中单位每单位物理长度
  956. /// </returns>
  957. protected virtual string[] startUpRules(Dictionary<MeasurementUnit, double> rules)
  958. {
  959. return new string[] { };
  960. }
  961. public bool DrawGrid
  962. {
  963. get
  964. {
  965. return false;// this.gridRenderer.Visible;
  966. }
  967. set
  968. {
  969. /**
  970. if (this.gridRenderer.Visible != value)
  971. {
  972. this.gridRenderer.Visible = value;
  973. OnDrawGridChanged();
  974. }**/
  975. }
  976. }
  977. /// <summary>
  978. /// 获取标尺集合
  979. /// </summary>
  980. /// <returns></returns>
  981. protected virtual List<mic_rulers> Mic_rulersAll()
  982. {
  983. return new List<mic_rulers>();
  984. }
  985. [Browsable(false)]
  986. public override bool Focused
  987. {
  988. get
  989. {
  990. return base.Focused || panel.Focused || leftRuler.Focused || topRuler.Focused;
  991. }
  992. }
  993. public new BorderStyle BorderStyle
  994. {
  995. get
  996. {
  997. return this.panel.BorderStyle;
  998. }
  999. set
  1000. {
  1001. this.panel.BorderStyle = value;
  1002. }
  1003. }
  1004. private void Renderers_Invalidated(object sender, InvalidateEventArgs e)
  1005. {
  1006. Rectangle rect = SurfaceToClient(e.InvalidRect);
  1007. rect.Inflate(1, 1);
  1008. Invalidate(rect);
  1009. }
  1010. /// <summary>
  1011. /// 计算像素跟踪的点
  1012. /// </summary>
  1013. /// <param name="point"></param>
  1014. /// <returns></returns>
  1015. public Point CalcPixelPoint(Point point)
  1016. {
  1017. Rectangle rc = this.panel.ClientRectangle;
  1018. int width = (int)(this.compositionSurface.Width * this.scaleFactor.Ratio);
  1019. int height = (int)(this.compositionSurface.Height * this.scaleFactor.Ratio);
  1020. int x = (rc.Width < width) ? this.panel.AutoScrollPosition.X + offsetHalfW : (rc.Width - width) / 2;
  1021. int y = (rc.Height < height) ? this.panel.AutoScrollPosition.Y + offsetHalfH : (rc.Height - height) / 2;
  1022. point.X -= x;
  1023. point.Y -= y;
  1024. return this.ScaleFactor.UnscalePoint(point);
  1025. }
  1026. public Surface GetDoubleBuffer(Size size)
  1027. {
  1028. Surface localDBSurface = null;
  1029. Size oldSize = new Size(0, 0);
  1030. // If we already have a double buffer surface reference, but if that surface
  1031. // is already disposed then don't worry about it.
  1032. if (this.doubleBufferSurface != null && this.doubleBufferSurface.IsDisposed)
  1033. {
  1034. oldSize = this.doubleBufferSurface.Size;
  1035. this.doubleBufferSurface = null;
  1036. }
  1037. // If we already have a double buffer surface reference, but if that surface
  1038. // is too small, then nuke it.
  1039. if (this.doubleBufferSurface != null &&
  1040. (this.doubleBufferSurface.Width < size.Width || this.doubleBufferSurface.Height < size.Height))
  1041. {
  1042. oldSize = this.doubleBufferSurface.Size;
  1043. this.doubleBufferSurface.Dispose();
  1044. this.doubleBufferSurface = null;
  1045. doubleBufferSurfaceWeakRef = null;
  1046. }
  1047. // If we don't have a double buffer, then we'd better get one.
  1048. if (this.doubleBufferSurface != null)
  1049. {
  1050. // Got one!
  1051. localDBSurface = this.doubleBufferSurface;
  1052. }
  1053. else if (doubleBufferSurfaceWeakRef != null)
  1054. {
  1055. // First, try to get the one that's already shared amongst all SurfaceBox instances.
  1056. localDBSurface = doubleBufferSurfaceWeakRef.Target;
  1057. // If it's disposed, then forget about it.
  1058. if (localDBSurface != null && localDBSurface.IsDisposed)
  1059. {
  1060. oldSize = localDBSurface.Size;
  1061. localDBSurface = null;
  1062. doubleBufferSurfaceWeakRef = null;
  1063. }
  1064. }
  1065. // Make sure the surface is big enough.
  1066. if (localDBSurface != null && (localDBSurface.Width < size.Width || localDBSurface.Height < size.Height))
  1067. {
  1068. oldSize = localDBSurface.Size;
  1069. localDBSurface.Dispose();
  1070. localDBSurface = null;
  1071. doubleBufferSurfaceWeakRef = null;
  1072. }
  1073. // So, do we have a surface? If not then we'd better make one.
  1074. if (localDBSurface == null)
  1075. {
  1076. Size newSize = new Size(Math.Max(size.Width, oldSize.Width), Math.Max(size.Height, oldSize.Height));
  1077. localDBSurface = new Surface(newSize.Width, newSize.Height);
  1078. doubleBufferSurfaceWeakRef = new WeakReference<Surface>(localDBSurface);
  1079. }
  1080. this.doubleBufferSurface = localDBSurface;
  1081. Surface window = localDBSurface.CreateWindow(0, 0, size.Width, size.Height);
  1082. return window;
  1083. }
  1084. /*
  1085. public Surface GetDoubleBuffer1(Size size)
  1086. {
  1087. Surface localDBSurface = null;
  1088. Size oldSize = new Size(0, 0);
  1089. // If we already have a double buffer surface reference, but if that surface
  1090. // is already disposed then don't worry about it.
  1091. if (this.doubleBufferSurface1 != null && this.doubleBufferSurface1.IsDisposed)
  1092. {
  1093. oldSize = this.doubleBufferSurface1.Size;
  1094. this.doubleBufferSurface1 = null;
  1095. }
  1096. // If we already have a double buffer surface reference, but if that surface
  1097. // is too small, then nuke it.
  1098. if (this.doubleBufferSurface1 != null &&
  1099. (this.doubleBufferSurface1.Width < size.Width || this.doubleBufferSurface1.Height < size.Height))
  1100. {
  1101. oldSize = this.doubleBufferSurface1.Size;
  1102. this.doubleBufferSurface1.Dispose();
  1103. this.doubleBufferSurface1 = null;
  1104. doubleBufferSurfaceWeakRef1 = null;
  1105. }
  1106. // If we don't have a double buffer, then we'd better get one.
  1107. if (this.doubleBufferSurface1 != null)
  1108. {
  1109. // Got one!
  1110. localDBSurface = this.doubleBufferSurface1;
  1111. }
  1112. else if (doubleBufferSurfaceWeakRef1 != null)
  1113. {
  1114. // First, try to get the one that's already shared amongst all SurfaceBox instances.
  1115. localDBSurface = doubleBufferSurfaceWeakRef1.Target;
  1116. // If it's disposed, then forget about it.
  1117. if (localDBSurface != null && localDBSurface.IsDisposed)
  1118. {
  1119. oldSize = localDBSurface.Size;
  1120. localDBSurface = null;
  1121. doubleBufferSurfaceWeakRef1 = null;
  1122. }
  1123. }
  1124. // Make sure the surface is big enough.
  1125. if (localDBSurface != null && (localDBSurface.Width < size.Width || localDBSurface.Height < size.Height))
  1126. {
  1127. oldSize = localDBSurface.Size;
  1128. localDBSurface.Dispose();
  1129. localDBSurface = null;
  1130. doubleBufferSurfaceWeakRef1 = null;
  1131. }
  1132. // So, do we have a surface? If not then we'd better make one.
  1133. if (localDBSurface == null)
  1134. {
  1135. Size newSize = new Size(Math.Max(size.Width, oldSize.Width), Math.Max(size.Height, oldSize.Height));
  1136. localDBSurface = new Surface(newSize.Width, newSize.Height);
  1137. doubleBufferSurfaceWeakRef1 = new WeakReference<Surface>(localDBSurface);
  1138. }
  1139. this.doubleBufferSurface1 = localDBSurface;
  1140. Surface window = localDBSurface.CreateWindow(0, 0, size.Width, size.Height);
  1141. return window;
  1142. }
  1143. */
  1144. private class RenderContext
  1145. {
  1146. public Surface[] windows;
  1147. public Point[] offsets;
  1148. public Rectangle[] rects;
  1149. public DocumentView owner;
  1150. public WaitCallback waitCallback;
  1151. public int TopIndex = 0;
  1152. public void RenderThreadMethod1(object indexObject)
  1153. {
  1154. int index = (int)indexObject;
  1155. this.owner.rendererList.topList[TopIndex].Render(windows[index], offsets[index]);
  1156. //this.owner.rendererList.RenderTop(windows[index], offsets[index]);
  1157. this.windows[index].Dispose();
  1158. this.windows[index] = null;
  1159. }
  1160. public void RenderThreadMethod(object indexObject)
  1161. {
  1162. int index = (int)indexObject;
  1163. this.owner.rendererList.list[0].Render(windows[index], offsets[index]);
  1164. this.windows[index].Dispose();
  1165. this.windows[index] = null;
  1166. }
  1167. }
  1168. public unsafe void DrawArea(RenderArgs ra, Point offset)
  1169. {
  1170. if (compositionSurface == null)
  1171. {
  1172. return;
  1173. }
  1174. if (renderContext == null || (renderContext.windows != null && renderContext.windows.Length != Processor.LogicalCpuCount))
  1175. {
  1176. renderContext = new RenderContext();
  1177. //这里需要计算宽高
  1178. renderContext.owner = this;
  1179. renderContext.waitCallback = new WaitCallback(renderContext.RenderThreadMethod);
  1180. renderContext.windows = new Surface[Processor.LogicalCpuCount];
  1181. renderContext.offsets = new Point[Processor.LogicalCpuCount];
  1182. renderContext.rects = new Rectangle[Processor.LogicalCpuCount];
  1183. }
  1184. Utility.SplitRectangle(ra.Bounds, renderContext.rects);
  1185. for (int i = 0; i < renderContext.rects.Length; ++i)
  1186. {
  1187. if (renderContext.rects[i].Width > 0 && renderContext.rects[i].Height > 0)
  1188. {
  1189. renderContext.offsets[i] = new Point(renderContext.rects[i].X + offset.X, renderContext.rects[i].Y + offset.Y);
  1190. renderContext.windows[i] = ra.Surface.CreateWindow(renderContext.rects[i]);
  1191. }
  1192. else
  1193. {
  1194. renderContext.windows[i] = null;
  1195. }
  1196. }
  1197. for (int i = 0; i < renderContext.windows.Length; ++i)
  1198. {
  1199. if (renderContext.windows[i] != null)
  1200. {
  1201. this.threadPool.QueueUserWorkItem(renderContext.waitCallback, BoxedConstants.GetInt32(i));
  1202. }
  1203. }
  1204. try
  1205. {
  1206. this.threadPool.Drain();
  1207. }
  1208. catch { }
  1209. }
  1210. public unsafe void DrawArea1(RenderArgs ra, Point offset, int TopIndex)
  1211. {
  1212. if (compositionSurface == null)
  1213. {
  1214. return;
  1215. }
  1216. if (renderContext_Phase == null || (renderContext_Phase.windows != null && renderContext_Phase.windows.Length != Processor.LogicalCpuCount))
  1217. {
  1218. renderContext_Phase = new RenderContext();
  1219. //这里需要计算宽高
  1220. renderContext_Phase.owner = this;
  1221. renderContext_Phase.waitCallback = new WaitCallback(renderContext_Phase.RenderThreadMethod1);
  1222. renderContext_Phase.windows = new Surface[1];//Processor.LogicalCpuCount
  1223. renderContext_Phase.offsets = new Point[1];
  1224. renderContext_Phase.rects = new Rectangle[1];
  1225. }
  1226. renderContext_Phase.TopIndex = TopIndex;
  1227. Utility.SplitRectangle(ra.Bounds, renderContext_Phase.rects);
  1228. for (int i = 0; i < renderContext_Phase.rects.Length; ++i)
  1229. {
  1230. if (renderContext_Phase.rects[i].Width > 0 && renderContext_Phase.rects[i].Height > 0)
  1231. {
  1232. renderContext_Phase.offsets[i] = new Point(renderContext_Phase.rects[i].X + offset.X, renderContext_Phase.rects[i].Y + offset.Y);
  1233. renderContext_Phase.windows[i] = ra.Surface.CreateWindow(renderContext_Phase.rects[i]);
  1234. }
  1235. else
  1236. {
  1237. renderContext_Phase.windows[i] = null;
  1238. }
  1239. }
  1240. for (int i = 0; i < renderContext_Phase.windows.Length; ++i)
  1241. {
  1242. if (renderContext_Phase.windows[i] != null)
  1243. {
  1244. this.threadPool.QueueUserWorkItem(renderContext_Phase.waitCallback, BoxedConstants.GetInt32(i));
  1245. }
  1246. }
  1247. try
  1248. {
  1249. this.threadPool.Drain();
  1250. }
  1251. catch { }
  1252. }
  1253. protected override void OnLoad(EventArgs e)
  1254. {
  1255. base.OnLoad(e);
  1256. // Sometimes OnLoad() gets called *twice* for some reason.
  1257. // See bug #1415 for the symptoms.
  1258. /**if (!this.hookedMouseEvents)
  1259. {
  1260. this.hookedMouseEvents = true;
  1261. foreach (Control c in Controls)
  1262. {
  1263. HookMouseEvents(c);
  1264. }
  1265. }**/
  1266. this.panel.Select();
  1267. }
  1268. public void HookMouseEvents()
  1269. {
  1270. if (!this.hookedMouseEvents)
  1271. {
  1272. this.hookedMouseEvents = true;
  1273. foreach (Control c in Controls)
  1274. {
  1275. HookMouseEvents(c);
  1276. }
  1277. }
  1278. }
  1279. public void PerformMouseWheel(Control sender, MouseEventArgs e)
  1280. {
  1281. HandleMouseWheel(sender, e);
  1282. }
  1283. protected override void OnMouseWheel(MouseEventArgs e)
  1284. {
  1285. HandleMouseWheel(this, e);
  1286. base.OnMouseWheel(e);
  1287. }
  1288. /// <summary>
  1289. /// 鼠标滚轮事件
  1290. /// </summary>
  1291. /// <param name="sender"></param>
  1292. /// <param name="e"></param>
  1293. protected virtual void HandleMouseWheel(Control sender, MouseEventArgs e)
  1294. {
  1295. double docDelta = (double)e.Delta / this.ScaleFactor.Ratio;
  1296. double oldX = this.DocumentScrollPositionF.X;
  1297. double oldY = this.DocumentScrollPositionF.Y;
  1298. double newX;
  1299. double newY;
  1300. if (Control.ModifierKeys == Keys.Shift)
  1301. {
  1302. this.panel.Response = false;
  1303. // scroll horizontally
  1304. newX = this.DocumentScrollPositionF.X - docDelta;
  1305. newY = this.DocumentScrollPositionF.Y;
  1306. }
  1307. else if (Control.ModifierKeys == Keys.None)
  1308. {
  1309. this.panel.Response = true;
  1310. // scroll vertically
  1311. newX = this.DocumentScrollPositionF.X;
  1312. newY = this.DocumentScrollPositionF.Y - docDelta;
  1313. }
  1314. else
  1315. {
  1316. this.panel.Response = false;
  1317. // no change
  1318. newX = this.DocumentScrollPositionF.X;
  1319. newY = this.DocumentScrollPositionF.Y;
  1320. }
  1321. if (newX != oldX || newY != oldY)
  1322. {
  1323. this.DocumentScrollPositionF = new PointF((float)newX, (float)newY);
  1324. UpdateRulerOffsets();
  1325. }
  1326. }
  1327. public override bool IsMouseCaptured()
  1328. {
  1329. return panel.Capture || leftRuler.Capture || topRuler.Capture;//this.Capture ||
  1330. }
  1331. /// <summary>
  1332. /// Get or set upper left of scroll location in document coordinates.
  1333. /// </summary>
  1334. [Browsable(false)]
  1335. public PointF DocumentScrollPositionF
  1336. {
  1337. get
  1338. {
  1339. if (this.panel == null)
  1340. {
  1341. return PointF.Empty;
  1342. }
  1343. else
  1344. {
  1345. return this.panel.ScrollPosition;
  1346. //return VisibleDocumentRectangleF.Location;
  1347. }
  1348. }
  1349. set
  1350. {
  1351. if (panel == null)
  1352. {
  1353. return;
  1354. }
  1355. else
  1356. {
  1357. this.panel.ScrollPosition = new Point((int)value.X, (int)value.Y);
  1358. }
  1359. /**
  1360. PointF sbClientF = this.panel.SurfaceToClient(value);
  1361. Point sbClient = Point.Round(sbClientF);
  1362. if (this.panel.AutoScrollPosition != new Point(-sbClient.X, -sbClient.Y))
  1363. {
  1364. this.panel.AutoScrollPosition = sbClient;
  1365. UpdateRulerOffsets();
  1366. this.topRuler.Invalidate();
  1367. this.leftRuler.Invalidate();
  1368. }**/
  1369. }
  1370. }
  1371. public Point PanelScrollPosition
  1372. {
  1373. get
  1374. {
  1375. return this.panel.ScrollPosition;
  1376. }
  1377. set
  1378. {
  1379. this.panel.ScrollPosition = value;
  1380. }
  1381. }
  1382. [Browsable(false)]
  1383. public PointF DocumentCenterPointF
  1384. {
  1385. get
  1386. {
  1387. RectangleF vsb = VisibleDocumentRectangleF;
  1388. PointF centerPt = new PointF((vsb.Left + vsb.Right) / 2, (vsb.Top + vsb.Bottom) / 2);
  1389. return centerPt;
  1390. }
  1391. set
  1392. {
  1393. RectangleF vsb = VisibleDocumentRectangleF;
  1394. PointF newCornerPt = new PointF(value.X - (vsb.Width / 2), value.Y - (vsb.Height / 2));
  1395. this.DocumentScrollPositionF = newCornerPt;
  1396. }
  1397. }
  1398. /// <summary>
  1399. /// Clean up any resources being used.
  1400. /// </summary>
  1401. protected override void Dispose(bool disposing)
  1402. {
  1403. if (disposing)
  1404. {
  1405. if (this.components != null)
  1406. {
  1407. this.components.Dispose();
  1408. this.components = null;
  1409. }
  1410. if (this.compositionSurface != null)
  1411. {
  1412. this.compositionSurface.Dispose();
  1413. this.compositionSurface = null;
  1414. }
  1415. }
  1416. base.Dispose(disposing);
  1417. }
  1418. /// <summary>
  1419. /// 缩放比例改变事件
  1420. /// </summary>
  1421. public event EventHandler ScaleFactorChanged;
  1422. protected virtual void OnScaleFactorChanged()
  1423. {
  1424. if (ScaleFactorChanged != null)
  1425. {
  1426. ScaleFactorChanged(this, EventArgs.Empty);
  1427. }
  1428. }
  1429. /// <summary>
  1430. /// 像素网格是否绘制的bool标记改变事件
  1431. /// </summary>
  1432. public event EventHandler DrawGridChanged;
  1433. protected virtual void OnDrawGridChanged()
  1434. {
  1435. if (DrawGridChanged != null)
  1436. {
  1437. DrawGridChanged(this, EventArgs.Empty);
  1438. }
  1439. }
  1440. /// <summary>
  1441. /// 合适大小
  1442. /// </summary>
  1443. public void ZoomToWindow()
  1444. {
  1445. if (this.document != null)
  1446. {
  1447. Rectangle max = ClientRectangleMax;
  1448. ScaleFactor zoom = ScaleFactor.Min(max.Width - 10,
  1449. document.Width,
  1450. max.Height - 10,
  1451. document.Height,
  1452. ScaleFactor.MinValue);
  1453. ScaleFactor min = ScaleFactor.Min(zoom, ScaleFactor.OneToOne);
  1454. this.ScaleFactor = min;
  1455. }
  1456. }
  1457. /// <summary>
  1458. /// 合适宽度
  1459. /// </summary>
  1460. public void ZoomToWidth()
  1461. {
  1462. if (this.document != null)
  1463. {
  1464. Rectangle max = ClientRectangleMax;
  1465. ScaleFactor zoom = ScaleFactor.UseIfValid(max.Width - 40, document.Width, ScaleFactor.MinValue);
  1466. ScaleFactor min = ScaleFactor.Min(zoom, ScaleFactor.OneToOne);
  1467. this.ScaleFactor = min;
  1468. }
  1469. }
  1470. /// <summary>
  1471. /// 合适高度
  1472. /// </summary>
  1473. public void ZoomToHeight()
  1474. {
  1475. if (this.document != null)
  1476. {
  1477. Rectangle max = ClientRectangleMax;
  1478. ScaleFactor zoom = ScaleFactor.UseIfValid(max.Height - 20, document.Height, ScaleFactor.MinValue);
  1479. ScaleFactor min = ScaleFactor.Min(zoom, ScaleFactor.OneToOne);
  1480. this.ScaleFactor = min;
  1481. }
  1482. }
  1483. private double GetZoomInFactorEpsilon()
  1484. {
  1485. double ratio = this.ScaleFactor.Ratio;
  1486. return (ratio + 0.01) / ratio;
  1487. /**
  1488. // Increase ratio by 1 percentage point
  1489. double currentRatio = this.ScaleFactor.Ratio;
  1490. double factor1 = (currentRatio + 0.01) / currentRatio;
  1491. // Increase ratio so that we increase our view by 1 pixel
  1492. double ratioW = (double)(this.panel.Width + 1) / (double)this.compositionSurface.Width;
  1493. double ratioH = (double)(this.panel.Height + 1) / (double)this.compositionSurface.Height;
  1494. double ratio = Math.Max(ratioW, ratioH);
  1495. double factor2 = ratio / currentRatio;
  1496. double factor = Math.Max(factor1, factor2);
  1497. return factor;
  1498. **/
  1499. }
  1500. private double GetZoomOutFactorEpsilon()
  1501. {
  1502. double ratio = this.ScaleFactor.Ratio;
  1503. return (ratio - 0.01) / ratio;
  1504. }
  1505. public virtual void ZoomIn(double factor)
  1506. {
  1507. Do.TryBool(() => ZoomInImpl(factor));
  1508. }
  1509. private void ZoomInImpl(double factor)
  1510. {
  1511. PointF centerPt = this.DocumentCenterPointF;
  1512. ScaleFactor oldSF = this.ScaleFactor;
  1513. ScaleFactor newSF = this.ScaleFactor;
  1514. int countdown = 3;
  1515. // At a minimum we want to increase the size of visible document by 1 pixel
  1516. // Figure out what the ratio of ourSize : ourSize+1 is, and start out with that
  1517. double zoomInEps = GetZoomInFactorEpsilon();
  1518. double desiredFactor = Math.Max(factor, zoomInEps);
  1519. double newFactor = desiredFactor;
  1520. // Keep setting the ScaleFactor until it actually 'sticks'
  1521. // Important for certain image sizes where not all zoom levels create distinct
  1522. // screen sizes
  1523. do
  1524. {
  1525. newSF = ScaleFactor.FromDouble(newSF.Ratio * newFactor);
  1526. this.ScaleFactor = newSF;
  1527. --countdown;
  1528. newFactor *= 1.10;
  1529. } while (this.ScaleFactor == oldSF && countdown > 0);
  1530. this.DocumentCenterPointF = centerPt;
  1531. }
  1532. public virtual void ZoomIn()
  1533. {
  1534. Do.TryBool(ZoomInImpl);
  1535. }
  1536. private void ZoomInImpl()
  1537. {
  1538. PointF centerPt = this.DocumentCenterPointF;
  1539. ScaleFactor oldSF = this.ScaleFactor;
  1540. ScaleFactor newSF = this.ScaleFactor;
  1541. int countdown = ScaleFactor.PresetValues.Length;
  1542. // Keep setting the ScaleFactor until it actually 'sticks'
  1543. // Important for certain image sizes where not all zoom levels create distinct
  1544. // screen sizes
  1545. do
  1546. {
  1547. newSF = newSF.GetNextLarger();
  1548. this.ScaleFactor = newSF;
  1549. --countdown;
  1550. } while (this.ScaleFactor == oldSF && countdown > 0);
  1551. this.DocumentCenterPointF = centerPt;
  1552. }
  1553. public virtual void ZoomOut(double factor)
  1554. {
  1555. Do.TryBool(() => ZoomOutImpl(factor));
  1556. }
  1557. private void ZoomOutImpl(double factor)
  1558. {
  1559. PointF centerPt = this.DocumentCenterPointF;
  1560. ScaleFactor oldSF = this.ScaleFactor;
  1561. ScaleFactor newSF = this.ScaleFactor;
  1562. int countdown = 3;
  1563. // At a minimum we want to decrease the size of visible document by 1 pixel (without dividing by zero of course)
  1564. // Figure out what the ratio of ourSize : ourSize-1 is, and start out with that
  1565. double zoomOutEps = GetZoomOutFactorEpsilon();
  1566. double factorRecip = 1.0 / factor;
  1567. double desiredFactor = Math.Min(factorRecip, zoomOutEps);
  1568. double newFactor = desiredFactor;
  1569. // Keep setting the ScaleFactor until it actually 'sticks'
  1570. // Important for certain image sizes where not all zoom levels create distinct
  1571. // screen sizes
  1572. do
  1573. {
  1574. newSF = ScaleFactor.FromDouble(newSF.Ratio * newFactor);
  1575. this.ScaleFactor = newSF;
  1576. --countdown;
  1577. newFactor *= 0.9;
  1578. } while (this.ScaleFactor == oldSF && countdown > 0);
  1579. this.DocumentCenterPointF = centerPt;
  1580. }
  1581. public virtual void ZoomOut()
  1582. {
  1583. Do.TryBool(ZoomOutImpl);
  1584. }
  1585. private void ZoomOutImpl()
  1586. {
  1587. PointF centerPt = this.DocumentCenterPointF;
  1588. ScaleFactor oldSF = this.ScaleFactor;
  1589. ScaleFactor newSF = this.ScaleFactor;
  1590. int countdown = ScaleFactor.PresetValues.Length;
  1591. // Keep setting the ScaleFactor until it actually 'sticks'
  1592. // Important for certain image sizes where not all zoom levels create distinct
  1593. // screen sizes
  1594. do
  1595. {
  1596. newSF = newSF.GetNextSmaller();
  1597. this.ScaleFactor = newSF;
  1598. --countdown;
  1599. } while (this.ScaleFactor == oldSF && countdown > 0);
  1600. this.DocumentCenterPointF = centerPt;
  1601. }
  1602. [Browsable(false)]
  1603. public ScaleFactor ScaleFactor
  1604. {
  1605. get
  1606. {
  1607. return this.scaleFactor;
  1608. }
  1609. set
  1610. {
  1611. UI.SuspendControlPainting(this);
  1612. ScaleFactor newValue = ScaleFactor.Min(value, ScaleFactor.MaxValue);
  1613. if (newValue == this.scaleFactor &&
  1614. this.scaleFactor == ScaleFactor.OneToOne)
  1615. {
  1616. // this space intentionally left blank
  1617. }
  1618. else
  1619. {
  1620. RectangleF visibleRect = this.VisibleDocumentRectangleF;
  1621. ScaleFactor oldSF = scaleFactor;
  1622. scaleFactor = newValue;
  1623. // This value is used later below to re-center the document on screen
  1624. PointF centerPt = new PointF(visibleRect.X + visibleRect.Width / 2,
  1625. visibleRect.Y + visibleRect.Height / 2);
  1626. if (compositionSurface != null)
  1627. {
  1628. Rectangle rc = this.panel.ClientRectangle;
  1629. int width = (int)(this.compositionSurface.Width * this.scaleFactor.Ratio);
  1630. int height = (int)(this.compositionSurface.Height * this.scaleFactor.Ratio);
  1631. if (rc.Width < width || rc.Height < height)
  1632. {
  1633. this.panel.AutoScrollMinSize = new Size((int)(this.compositionSurface.Width * scaleFactor.Ratio) + offsetW, (int)(this.compositionSurface.Height * scaleFactor.Ratio) + offsetH);
  1634. //////求因缩放产生的位移,进行补偿,实现锚点缩放的效果
  1635. ////VX = (int)((double)x * (ow - pictureBox1.Width) / ow);
  1636. ////VY = (int)((double)y * (oh - pictureBox1.Height) / oh);
  1637. ////pictureBox1.Location = new Point(pictureBox1.Location.X + VX, pictureBox1.Location.Y + VY);
  1638. //this.panel.AutoScrollPosition = new Point((int)(width - rc.Width) / 2 + 150, (int)(height - rc.Height) / 2 + 75);
  1639. //////this.panel.AutoScrollPosition = sbClient;
  1640. ////UpdateRulerOffsets();
  1641. ////this.topRuler.Invalidate();
  1642. ////this.leftRuler.Invalidate();
  1643. }
  1644. else
  1645. {
  1646. this.panel.AutoScrollMinSize = new Size((int)(this.compositionSurface.Width * scaleFactor.Ratio), (int)(this.compositionSurface.Height * scaleFactor.Ratio));
  1647. //this.panel.AutoScrollPosition = new Point((int)(width - rc.Width) / 2 + 0, (int)(height - rc.Height) / 2 + 0);
  1648. ////UpdateRulerOffsets();
  1649. ////this.topRuler.Invalidate();
  1650. ////this.leftRuler.Invalidate();
  1651. }
  1652. this.panel.ScaleFactor = this.scaleFactor;
  1653. if (leftRuler != null)
  1654. {
  1655. this.leftRuler.ScaleFactor = scaleFactor;
  1656. }
  1657. if (topRuler != null)
  1658. {
  1659. this.topRuler.ScaleFactor = scaleFactor;
  1660. }
  1661. }
  1662. // re center ourself
  1663. RectangleF visibleRect2 = this.VisibleDocumentRectangleF;
  1664. RecenterView(centerPt);
  1665. }
  1666. this.OnResize(EventArgs.Empty);
  1667. this.OnScaleFactorChanged();
  1668. if (this.appWorkspace != null && refueshZoomTrackValue && this.rightDown)
  1669. {
  1670. this.appWorkspace.SetZoonTrackValue(newValue);
  1671. }
  1672. /*
  1673. //设置底部缩放比列的百分比tracker的值
  1674. this.panelBottom.trackBar.Maximum = (int)(ScaleFactor.MaxValue.Ratio * 100);
  1675. if (newValue != null)
  1676. {
  1677. this.PanelBottom.trackBar.ValueChanged -= this.PanelBottom_trackBar_ValueChanged;
  1678. this.PanelBottom.trackBar.Value = (int)(newValue.Ratio * 100);
  1679. this.PanelBottom.trackBar.ValueChanged += new EventHandler(this.PanelBottom_trackBar_ValueChanged);
  1680. }
  1681. //设置底部缩放比例的百分比textbox的值
  1682. this.PanelBottom.textBox.Text = newValue.ToString();
  1683. */
  1684. if (compositionSurface != null)
  1685. this.rendererList.DestinationSize = this.scaleFactor.ScaleSize(compositionSurface.Size);
  1686. UI.ResumeControlPainting(this);
  1687. Invalidate(true);
  1688. }
  1689. }
  1690. /// <summary>
  1691. /// Returns a rectangle for the bounding rectangle of what is currently visible on screen,
  1692. /// in document coordinates.
  1693. /// </summary>
  1694. [Browsable(false)]
  1695. public RectangleF VisibleDocumentRectangleF
  1696. {
  1697. get
  1698. {
  1699. Rectangle panelRect = this.panel.RectangleToScreen(this.panel.ClientRectangle); // screen coords coords
  1700. Rectangle docScreenRect = panelRect; // screen coords
  1701. Rectangle docClientRect = RectangleToClient(docScreenRect);
  1702. RectangleF docDocRectF = ClientToDocument(docClientRect);
  1703. return docDocRectF;
  1704. }
  1705. }
  1706. public Rectangle PanelClientRectangle
  1707. {
  1708. get
  1709. {
  1710. return panel.ClientRectangle;
  1711. }
  1712. }
  1713. public int SurfaceScrollableWidth
  1714. {
  1715. get
  1716. {
  1717. return (int)(this.compositionSurface.Width * this.scaleFactor.Ratio);
  1718. }
  1719. }
  1720. public int SurfaceScrollableHeight
  1721. {
  1722. get
  1723. {
  1724. return (int)(this.compositionSurface.Height * this.scaleFactor.Ratio);
  1725. }
  1726. }
  1727. public double ScaleRatio
  1728. {
  1729. get
  1730. {
  1731. return this.scaleFactor.Ratio;
  1732. }
  1733. }
  1734. /// <summary>
  1735. /// Returns a rectangle in <b>screen</b> coordinates that represents the space taken up
  1736. /// by the document that is visible on screen.
  1737. /// </summary>
  1738. [Browsable(false)]
  1739. public Rectangle VisibleDocumentBounds
  1740. {
  1741. get
  1742. {
  1743. // convert coordinates: document -> client -> screen
  1744. return RectangleToScreen(Utility.RoundRectangle(DocumentToClient(VisibleDocumentRectangleF)));
  1745. }
  1746. }
  1747. /// <summary>
  1748. /// Returns a rectangle in client coordinates that denotes the space that the document
  1749. /// may take up. This is essentially the ClientRectangle converted to screen coordinates
  1750. /// and then with the rulers and scrollbars subtracted out.
  1751. /// </summary>
  1752. public Rectangle VisibleViewRectangle
  1753. {
  1754. get
  1755. {
  1756. Rectangle clientRect = this.panel.ClientRectangle;
  1757. Rectangle screenRect = this.panel.RectangleToScreen(clientRect);
  1758. Rectangle ourClientRect = RectangleToClient(screenRect);
  1759. return ourClientRect;
  1760. }
  1761. }
  1762. public Rectangle ClientRectangleMax
  1763. {
  1764. get
  1765. {
  1766. return RectangleToClient(this.panel.RectangleToScreen(this.panel.Bounds));
  1767. }
  1768. }
  1769. public Rectangle ClientRectangleMin
  1770. {
  1771. get
  1772. {
  1773. Rectangle bounds = ClientRectangleMax;
  1774. bounds.Width -= SystemInformation.VerticalScrollBarWidth;
  1775. bounds.Height -= SystemInformation.HorizontalScrollBarHeight;
  1776. return bounds;
  1777. }
  1778. }
  1779. public void SetHighlightRectangle(RectangleF rectF)
  1780. {
  1781. if (rectF.Width == 0 || rectF.Height == 0)
  1782. {
  1783. this.leftRuler.HighlightEnabled = false;
  1784. this.topRuler.HighlightEnabled = false;
  1785. }
  1786. else
  1787. {
  1788. if (this.topRuler != null)
  1789. {
  1790. this.topRuler.HighlightEnabled = true;
  1791. this.topRuler.HighlightStart = rectF.Left;
  1792. this.topRuler.HighlightLength = rectF.Width;
  1793. }
  1794. if (this.leftRuler != null)
  1795. {
  1796. this.leftRuler.HighlightEnabled = true;
  1797. this.leftRuler.HighlightStart = rectF.Top;
  1798. this.leftRuler.HighlightLength = rectF.Height;
  1799. }
  1800. }
  1801. }
  1802. /// <summary>
  1803. /// Gets or sets the Document that is shown through this instance of DocumentView.
  1804. /// </summary>
  1805. /// <remarks>
  1806. /// This property is thread safe and may be called from a non-UI thread. However,
  1807. /// if the setter is called from a non-UI thread, then that thread will block as
  1808. /// the call is marshaled to the UI thread.
  1809. /// </remarks>
  1810. [Browsable(false)]
  1811. public Document Document
  1812. {
  1813. get
  1814. {
  1815. return document;
  1816. }
  1817. set
  1818. {
  1819. if (InvokeRequired)
  1820. {
  1821. this.Invoke(new Procedure<Document, bool>(DocumentSetImpl), new object[2] { value, true });
  1822. }
  1823. else
  1824. {
  1825. DocumentSetImpl(value, true);
  1826. }
  1827. }
  1828. }
  1829. public void setDoc(Document document, bool toeditori)
  1830. {
  1831. if (InvokeRequired)
  1832. {
  1833. this.Invoke(new Procedure<Document, bool>(DocumentSetImpl), new object[2] { document, toeditori });
  1834. }
  1835. else
  1836. {
  1837. DocumentSetImpl(document, toeditori);
  1838. }
  1839. }
  1840. private void DocumentSetImpl(Document value, bool toedit)
  1841. {
  1842. this.DoubleBuffered = true;
  1843. SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
  1844. PointF dspf = DocumentScrollPositionF;
  1845. OnDocumentChanging(value);
  1846. try
  1847. {
  1848. if (this.document != null)
  1849. {
  1850. this.document.Invalidated -= Document_Invalidated;
  1851. this.document.Metadata.Changed -= DocumentMetaDataChangedHandler;
  1852. }
  1853. this.document = value;
  1854. if (document != null)
  1855. {
  1856. if (this.compositionSurface != null &&
  1857. this.compositionSurface.Size != document.Size)
  1858. {
  1859. this.compositionSurface.Dispose();
  1860. this.compositionSurface = null;
  1861. }
  1862. //if (!toedit && this.compositionSurface.Thumborigin != null)
  1863. // document.surface.Thumborigin = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(OpenCvSharp.Extensions.BitmapConverter.ToMat(this.compositionSurface.Thumborigin));
  1864. this.compositionSurface = document.surface;
  1865. if (IsCreatThumb)
  1866. {
  1867. //生成缩略图
  1868. this.compositionSurface.CreateThumbnail();
  1869. //生成原图的备份
  1870. //if (toedit)
  1871. // this.compositionSurface.CreateThumborigin();
  1872. }
  1873. this.baseRenderer.Source = document.surface;
  1874. if (this.compositionSurface != null)
  1875. {
  1876. this.rendererList.SourceSize = this.compositionSurface.Size;
  1877. this.rendererList.DestinationSize = this.Size;
  1878. }
  1879. this.document.Invalidated += Document_Invalidated;
  1880. this.document.Metadata.Changed += DocumentMetaDataChangedHandler;
  1881. }
  1882. Invalidate(true);
  1883. DocumentMetaDataChangedHandler(this, EventArgs.Empty);
  1884. this.OnResize(EventArgs.Empty);
  1885. OnDocumentChanged();
  1886. }
  1887. finally
  1888. {
  1889. }
  1890. DocumentScrollPositionF = dspf;
  1891. }
  1892. public bool PanelAutoScroll
  1893. {
  1894. get
  1895. {
  1896. return panel.AutoScroll;
  1897. }
  1898. set
  1899. {
  1900. if (panel.AutoScroll != value)
  1901. {
  1902. panel.AutoScroll = value;
  1903. }
  1904. }
  1905. }
  1906. public DrawToolType ActiveTool
  1907. {
  1908. get => _activeTool;
  1909. set
  1910. {
  1911. if (!tools.ContainsKey(value)) return;
  1912. if (value == DrawToolType.DrawAutoRuler || value == DrawToolType.DrawPrestoredRuler || value == DrawToolType.DrawDateMark || value == DrawToolType.DrawTimeMark || value == DrawToolType.DrawGainNumber)
  1913. {
  1914. tools[value].InvokeMember("addWithNewObject",
  1915. BindingFlags.Public |
  1916. BindingFlags.Static |
  1917. BindingFlags.InvokeMethod,
  1918. null,
  1919. null,
  1920. new object[1] { this });
  1921. }
  1922. else {
  1923. tools[_activeTool].InvokeMember("beginWithNewObject",
  1924. BindingFlags.Public |
  1925. BindingFlags.Static |
  1926. BindingFlags.InvokeMethod,
  1927. null,
  1928. null,
  1929. new object[0] { });
  1930. }
  1931. //tools[this.activeTool].beginWithNewObject();
  1932. if (value != DrawToolType.Pointer && value != DrawToolType.InclusionNoEffect && this.appWorkspace.GetScriptRunning())
  1933. this.appWorkspace.SetScriptStopping(true);
  1934. else if (value == DrawToolType.Pointer)
  1935. {
  1936. this.appWorkspace.SetScriptStopping(false);
  1937. //this.appWorkspace.ResumeScriptRunning();
  1938. }
  1939. _activeTool = value;
  1940. }
  1941. }
  1942. private void HookMouseEvents(Control c)
  1943. {
  1944. /**
  1945. if (this.inkAvailable)
  1946. {
  1947. // This must be in a separate function, otherwise we will throw an exception when JITting
  1948. // because MS.Ink.dll won't be available
  1949. // This is to support systems that don't have ink installed
  1950. try
  1951. {
  1952. Ink.HookInk(this, c);
  1953. }
  1954. catch (InvalidOperationException ioex)
  1955. {
  1956. Tracing.Ping("Exception while initializing ink hooks: " + ioex.ToString());
  1957. this.inkAvailable = false;
  1958. }
  1959. }**/
  1960. //暂时注释掉
  1961. /**
  1962. c.MouseEnter += new EventHandler(this.MouseEnterHandler);
  1963. c.MouseLeave += new EventHandler(this.MouseLeaveHandler);
  1964. c.MouseUp += new MouseEventHandler(this.MouseUpHandler);
  1965. c.MouseMove += new MouseEventHandler(this.MouseMoveHandler);
  1966. c.MouseDown += new MouseEventHandler(this.MouseDownHandler);
  1967. c.Click += new EventHandler(this.ClickHandler);
  1968. foreach (Control c2 in c.Controls)
  1969. {
  1970. HookMouseEvents(c2);
  1971. }**/
  1972. }
  1973. private void UpdateRulerOffsets()
  1974. {
  1975. this.topRuler.Offset = ScaleFactor.UnscaleScalar(UI.ScaleWidth(-16.0f) - this.panel.Location.X);
  1976. this.topRuler.Update();
  1977. this.leftRuler.Offset = ScaleFactor.UnscaleScalar(0.0f - this.panel.Location.Y);
  1978. this.leftRuler.Update();
  1979. }
  1980. private void DoLayout()
  1981. {
  1982. if (panel.ClientRectangle != new Rectangle(0, 0, 0, 0))
  1983. {
  1984. int newX = panel.AutoScrollPosition.X;
  1985. int newY = panel.AutoScrollPosition.Y;
  1986. if (panel.ClientRectangle.Width > this.panel.Width)
  1987. {
  1988. newX = panel.AutoScrollPosition.X + ((panel.ClientRectangle.Width) / 2);
  1989. }
  1990. if (panel.ClientRectangle.Height > this.panel.Height)
  1991. {
  1992. newY = panel.AutoScrollPosition.Y + ((panel.ClientRectangle.Height) / 2);
  1993. }
  1994. }
  1995. //暂时注释掉,因为在预览窗口、位置导航等显示标尺的时候,会闪烁,不知道会不会有啥问题
  1996. this.UpdateRulerOffsets();
  1997. this.UpdateImgLocation();
  1998. }
  1999. private void CheckForFirstInputAfterGotFocus()
  2000. {
  2001. if (this.raiseFirstInputAfterGotFocus)
  2002. {
  2003. this.raiseFirstInputAfterGotFocus = false;
  2004. OnFirstInputAfterGotFocus();
  2005. }
  2006. }
  2007. private void Document_Invalidated(object sender, InvalidateEventArgs e)
  2008. {
  2009. if (this.ScaleFactor == ScaleFactor.OneToOne)
  2010. {
  2011. this.panel.Invalidate(e.InvalidRect);
  2012. }
  2013. else
  2014. {
  2015. Rectangle inflatedInvalidRect = Rectangle.Inflate(e.InvalidRect, 1, 1);
  2016. Rectangle clientRect = this.panel.SurfaceToClient(inflatedInvalidRect);
  2017. Rectangle inflatedClientRect = Rectangle.Inflate(clientRect, 1, 1);
  2018. this.panel.Invalidate(inflatedClientRect);
  2019. }
  2020. }
  2021. protected void UpdateComposition(bool raiseEvent)
  2022. {
  2023. lock (this)
  2024. {
  2025. /*using (RenderArgs ra = new RenderArgs(this.compositionSurface))
  2026. {
  2027. bool result = this.document.Update(ra);
  2028. if (raiseEvent && (result || this.withheldCompositionUpdatedCount > 0))
  2029. {
  2030. OnCompositionUpdated();
  2031. if (!result && this.withheldCompositionUpdatedCount > 0)
  2032. {
  2033. --this.withheldCompositionUpdatedCount;
  2034. }
  2035. }
  2036. else if (!raiseEvent && result)
  2037. {
  2038. // If they want to not raise the event, we must keep track so that
  2039. // the next time UpdateComposition() is called we still raise this
  2040. // event even if Update() returned false (which indicates there
  2041. // was nothing to update)
  2042. ++this.withheldCompositionUpdatedCount;
  2043. }
  2044. }*/
  2045. }
  2046. }
  2047. /// <summary>
  2048. /// 重新设置中心点
  2049. /// </summary>
  2050. /// <param name="newCenter"></param>
  2051. public void RecenterView(PointF newCenter)
  2052. {
  2053. RectangleF visibleRect = VisibleDocumentRectangleF;
  2054. PointF cornerPt = new PointF(
  2055. newCenter.X - (visibleRect.Width / 2),
  2056. newCenter.Y - (visibleRect.Height / 2));
  2057. this.DocumentScrollPositionF = cornerPt;
  2058. }
  2059. /// <summary>
  2060. /// 文档元数据改变事件
  2061. /// </summary>
  2062. /// <param name="sender"></param>
  2063. /// <param name="e"></param>
  2064. private void DocumentMetaDataChangedHandler(object sender, EventArgs e)
  2065. {
  2066. if (this.document != null)
  2067. {
  2068. this.leftRuler.Dpu = 1 / document.PixelToPhysicalY(1, this.leftRuler.MeasurementUnit);
  2069. this.topRuler.Dpu = 1 / document.PixelToPhysicalY(1, this.topRuler.MeasurementUnit);
  2070. }
  2071. }
  2072. /// <summary>
  2073. /// 获取当前文档的视场、标注、测量的xml数据
  2074. /// </summary>
  2075. /// <param name="drawClass"></param>
  2076. /// <returns></returns>
  2077. public List<ViewModel> GetXmlFromViewOrLabelOrMeasure(DrawClass drawClass)
  2078. {
  2079. List<ViewModel> topLVMModels = new List<ViewModel>();
  2080. if (this.GraphicsList != null && this.GraphicsList.Count > 0)
  2081. {
  2082. for (int i = this.GraphicsList.Count - 1; i >= 0; i--)
  2083. {
  2084. ViewModel model = new ViewModel();
  2085. if (this.GraphicsList[i].objectType == drawClass)
  2086. {
  2087. model.Type = this.GraphicsList[i].drawToolType.ToString();
  2088. model.CombineMode = ((ViewBase)(this.GraphicsList[i])).combineMode.ToString();
  2089. model.Rectangle = this.GraphicsList[i].Rectangle;
  2090. model.Points = this.GraphicsList[i].GetPoints();
  2091. model.StartPoint = this.GraphicsList[i].startPoint;
  2092. model.EndPoint = this.GraphicsList[i].endPoint;
  2093. topLVMModels.Add(model);
  2094. }
  2095. }
  2096. }
  2097. return topLVMModels;
  2098. }
  2099. /// <summary>
  2100. /// 从打开视场窗口添加视场到DocumentView
  2101. /// </summary>
  2102. /// <param name="drawObject"></param>
  2103. public void AddGraphicsFromForm(DrawObject drawObject)
  2104. {
  2105. this.GraphicsList.UnselectAll();
  2106. this.GraphicsList.Add(drawObject);
  2107. this.Capture = true;
  2108. this.Refresh();
  2109. this.SetDirty();
  2110. }
  2111. #region 暂未用到的方法, 暂留
  2112. protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
  2113. {
  2114. Keys keyCode = keyData & Keys.KeyCode;
  2115. if (Utility.IsArrowKey(keyData) ||
  2116. keyCode == Keys.Delete ||
  2117. keyCode == Keys.Tab)
  2118. {
  2119. KeyEventArgs kea = new KeyEventArgs(keyData);
  2120. // We only intercept WM_KEYDOWN because WM_KEYUP is not sent!
  2121. switch (msg.Msg)
  2122. {
  2123. case 0x100: //NativeMethods.WmConstants.WM_KEYDOWN:
  2124. if (this.ContainsFocus)
  2125. {
  2126. OnDocumentKeyDown(kea);
  2127. //OnDocumentKeyUp(kea);
  2128. if (Utility.IsArrowKey(keyData))
  2129. {
  2130. kea.Handled = true;
  2131. }
  2132. }
  2133. if (kea.Handled)
  2134. {
  2135. return true;
  2136. }
  2137. break;
  2138. /*
  2139. case 0x101: //NativeMethods.WmConstants.WM_KEYUP:
  2140. if (this.ContainsFocus)
  2141. {
  2142. OnDocumentKeyUp(kea);
  2143. }
  2144. return kea.Handled;
  2145. */
  2146. }
  2147. }
  2148. return base.ProcessCmdKey(ref msg, keyData);
  2149. }
  2150. private void MouseEnterHandler(object sender, EventArgs e)
  2151. {
  2152. OnDocumentMouseEnter(EventArgs.Empty);
  2153. }
  2154. private void MouseLeaveHandler(object sender, EventArgs e)
  2155. {
  2156. OnDocumentMouseLeave(EventArgs.Empty);
  2157. }
  2158. private void MouseMoveHandler(object sender, MouseEventArgs e)
  2159. {
  2160. Point docPoint = MouseToDocument((Control)sender, new Point(e.X, e.Y));
  2161. PointF docPointF = MouseToDocumentF((Control)sender, new Point(e.X, e.Y));
  2162. if (RulersEnabled)
  2163. {
  2164. int x;
  2165. if (docPointF.X > 0)
  2166. {
  2167. x = (int)Math.Truncate(docPointF.X);
  2168. }
  2169. else if (docPointF.X < 0)
  2170. {
  2171. x = (int)Math.Truncate(docPointF.X - 1);
  2172. }
  2173. else // if (docPointF.X == 0)
  2174. {
  2175. x = 0;
  2176. }
  2177. int y;
  2178. if (docPointF.Y > 0)
  2179. {
  2180. y = (int)Math.Truncate(docPointF.Y);
  2181. }
  2182. else if (docPointF.Y < 0)
  2183. {
  2184. y = (int)Math.Truncate(docPointF.Y - 1);
  2185. }
  2186. else // if (docPointF.Y == 0)
  2187. {
  2188. y = 0;
  2189. }
  2190. topRuler.Value = x;
  2191. leftRuler.Value = y;
  2192. UpdateRulerOffsets();
  2193. }
  2194. OnDocumentMouseMove(new MouseEventArgs(e.Button, e.Clicks, docPoint.X, docPoint.Y, e.Delta));
  2195. }
  2196. private void MouseUpHandler(object sender, MouseEventArgs e)
  2197. {
  2198. if (sender is Ruler)
  2199. {
  2200. return;
  2201. }
  2202. Point docPoint = MouseToDocument((Control)sender, new Point(e.X, e.Y));
  2203. Point pt = panel.AutoScrollPosition;
  2204. panel.Focus();
  2205. OnDocumentMouseUp(new MouseEventArgs(e.Button, e.Clicks, docPoint.X, docPoint.Y, e.Delta));
  2206. }
  2207. private void MouseDownHandler(object sender, MouseEventArgs e)
  2208. {
  2209. if (sender is Ruler)
  2210. {
  2211. return;
  2212. }
  2213. Point docPoint = MouseToDocument((Control)sender, new Point(e.X, e.Y));
  2214. Point pt = panel.AutoScrollPosition;
  2215. panel.Focus();
  2216. OnDocumentMouseDown(new MouseEventArgs(e.Button, e.Clicks, docPoint.X, docPoint.Y, e.Delta));
  2217. }
  2218. private void ClickHandler(object sender, EventArgs e)
  2219. {
  2220. Point pt = panel.AutoScrollPosition;
  2221. panel.Focus();
  2222. OnDocumentClick();
  2223. }
  2224. #endregion
  2225. #region 对外的各种事件委托
  2226. // these events will report mouse coordinates in document space
  2227. // i.e. if the image is zoomed at 200% then the mouse coordinates will be divided in half
  2228. public event EventHandler CompositionUpdated;
  2229. private void OnCompositionUpdated()
  2230. {
  2231. if (CompositionUpdated != null)
  2232. {
  2233. CompositionUpdated(this, EventArgs.Empty);
  2234. }
  2235. }
  2236. public event EventHandler RulersEnabledChanged;
  2237. protected void OnRulersEnabledChanged()
  2238. {
  2239. if (RulersEnabledChanged != null)
  2240. {
  2241. RulersEnabledChanged(this, EventArgs.Empty);
  2242. }
  2243. }
  2244. public event EventHandler<EventArgs<Document>> DocumentChanging;
  2245. protected virtual void OnDocumentChanging(Document newDocument)
  2246. {
  2247. if (DocumentChanging != null)
  2248. {
  2249. DocumentChanging(this, new EventArgs<Document>(newDocument));
  2250. }
  2251. }
  2252. public event EventHandler DocumentChanged;
  2253. protected virtual void OnDocumentChanged()
  2254. {
  2255. if (DocumentChanged != null)
  2256. {
  2257. DocumentChanged(this, EventArgs.Empty);
  2258. }
  2259. }
  2260. public event EventHandler FirstInputAfterGotFocus;
  2261. protected virtual void OnFirstInputAfterGotFocus()
  2262. {
  2263. if (FirstInputAfterGotFocus != null)
  2264. {
  2265. FirstInputAfterGotFocus(this, EventArgs.Empty);
  2266. }
  2267. }
  2268. /// <summary>
  2269. /// Occurs when the mouse enters an element of the UI that is considered to be part of
  2270. /// the document space.
  2271. /// </summary>
  2272. public event EventHandler DocumentMouseEnter;
  2273. protected virtual void OnDocumentMouseEnter(EventArgs e)
  2274. {
  2275. if (DocumentMouseEnter != null)
  2276. {
  2277. DocumentMouseEnter(this, e);
  2278. }
  2279. }
  2280. /// <summary>
  2281. /// Occurs when the mouse leaves an element of the UI that is considered to be part of
  2282. /// the document space.
  2283. /// </summary>
  2284. /// <remarks>
  2285. /// This event being raised does not necessarily correpond to the mouse leaving
  2286. /// document space, only that it has left the screen space of an element of the UI
  2287. /// that is part of document space. For example, if the mouse leaves the canvas and
  2288. /// then enters the rulers, you will see a DocumentMouseLeave event raised which is
  2289. /// then immediately followed by a DocumentMouseEnter event.
  2290. /// </remarks>
  2291. public event EventHandler DocumentMouseLeave;
  2292. protected virtual void OnDocumentMouseLeave(EventArgs e)
  2293. {
  2294. if (DocumentMouseLeave != null)
  2295. {
  2296. DocumentMouseLeave(this, e);
  2297. }
  2298. }
  2299. /// <summary>
  2300. /// Occurs when the mouse or stylus point is moved over the document.
  2301. /// </summary>
  2302. /// <remarks>
  2303. /// Note: This event will always be raised twice in succession. One will provide a
  2304. /// MouseEventArgs, and the other will provide a StylusEventArgs. It is up to consumers
  2305. /// of this event to decide which one is pertinent and to then filter out the other
  2306. /// type of event.
  2307. /// </remarks>
  2308. public event MouseEventHandler DocumentMouseMove;
  2309. protected virtual void OnDocumentMouseMove(MouseEventArgs e)
  2310. {
  2311. if (DocumentMouseMove != null)
  2312. {
  2313. DocumentMouseMove(this, e);
  2314. }
  2315. }
  2316. /// <summary>
  2317. /// Occurs when the mouse or stylus point is over the document and a mouse button is released
  2318. /// or the stylus is lifted.
  2319. /// </summary>
  2320. /// <remarks>
  2321. /// Note: This event will always be raised twice in succession. One will provide a
  2322. /// MouseEventArgs, and the other will provide a StylusEventArgs. It is up to consumers
  2323. /// of this event to decide which one is pertinent and to then filter out the other
  2324. /// type of event.
  2325. /// </remarks>
  2326. public event MouseEventHandler DocumentMouseUp;
  2327. protected virtual void OnDocumentMouseUp(MouseEventArgs e)
  2328. {
  2329. CheckForFirstInputAfterGotFocus();
  2330. if (DocumentMouseUp != null)
  2331. {
  2332. DocumentMouseUp(this, e);
  2333. }
  2334. }
  2335. /// <summary>
  2336. /// Occurs when the mouse or stylus point is over the document and a mouse button or
  2337. /// stylus is pressed.
  2338. /// </summary>
  2339. /// <remarks>
  2340. /// Note: This event will always be raised twice in succession. One will provide a
  2341. /// MouseEventArgs, and the other will provide a StylusEventArgs. It is up to consumers
  2342. /// of this event to decide which one is pertinent and to then filter out the other
  2343. /// type of event.
  2344. /// </remarks>
  2345. public event MouseEventHandler DocumentMouseDown;
  2346. protected virtual void OnDocumentMouseDown(MouseEventArgs e)
  2347. {
  2348. CheckForFirstInputAfterGotFocus();
  2349. if (DocumentMouseDown != null)
  2350. {
  2351. DocumentMouseDown(this, e);
  2352. }
  2353. }
  2354. public event EventHandler DocumentClick;
  2355. protected void OnDocumentClick()
  2356. {
  2357. CheckForFirstInputAfterGotFocus();
  2358. if (DocumentClick != null)
  2359. {
  2360. DocumentClick(this, EventArgs.Empty);
  2361. }
  2362. }
  2363. public event KeyPressEventHandler DocumentKeyPress;
  2364. protected void OnDocumentKeyPress(KeyPressEventArgs e)
  2365. {
  2366. CheckForFirstInputAfterGotFocus();
  2367. if (DocumentKeyPress != null)
  2368. {
  2369. DocumentKeyPress(this, e);
  2370. }
  2371. }
  2372. public event KeyEventHandler DocumentKeyDown;
  2373. protected void OnDocumentKeyDown(KeyEventArgs e)
  2374. {
  2375. CheckForFirstInputAfterGotFocus();
  2376. if (DocumentKeyDown != null)
  2377. {
  2378. DocumentKeyDown(this, e);
  2379. }
  2380. }
  2381. public event KeyEventHandler DocumentKeyUp;
  2382. protected void OnDocumentKeyUp(KeyEventArgs e)
  2383. {
  2384. CheckForFirstInputAfterGotFocus();
  2385. if (DocumentKeyUp != null)
  2386. {
  2387. DocumentKeyUp(this, e);
  2388. }
  2389. }
  2390. #endregion
  2391. #region 多相相关
  2392. public List<PhaseModel> PhaseModels
  2393. {
  2394. set
  2395. {
  2396. //this.phaseModels.Clear();
  2397. //this.phaseModels.AddRanage(value);
  2398. this.phaseModels = value;
  2399. this.appWorkspace.GetPanelBottom().documentStrip.ClearPhase();
  2400. this.appWorkspace.GetPanelBottom().documentStrip.AddPhase(this.phaseModels);
  2401. this.Refresh();
  2402. }
  2403. get
  2404. {
  2405. return this.phaseModels;
  2406. }
  2407. }
  2408. /// <summary>
  2409. /// 将物相和二值的数据带入到图像索引列表
  2410. /// </summary>
  2411. public Bitmap BinarizationThumbnail
  2412. {
  2413. get
  2414. {
  2415. //二值化相关
  2416. if (this.phaseModels.Count == 0)
  2417. return this.CompositionSurface.Thumbnail;
  2418. else
  2419. {
  2420. Bitmap newBit = this.CompositionSurface.Thumbnail;
  2421. Graphics graphics = Graphics.FromImage(newBit);
  2422. for (int i = 0; i < this.phaseModels.Count; i++)
  2423. {
  2424. PhaseModel item = this.phaseModels[i];
  2425. if (item.choise && item.mat != null)
  2426. graphics.DrawImage(OpenCvSharp.Extensions.BitmapConverter.ToBitmap(item.mat), 0, 0, newBit.Width, newBit.Height);
  2427. }
  2428. return newBit;
  2429. }
  2430. }
  2431. }
  2432. /// <summary>
  2433. /// 将物相和二值的数据带入到分析页面
  2434. /// </summary>
  2435. public PhaseModel AnalysisPhaseModel
  2436. {
  2437. get
  2438. {
  2439. //二值化相关
  2440. if (this.phaseModels.Count == 0)
  2441. {
  2442. return null;
  2443. }
  2444. else
  2445. {
  2446. OpenCvSharp.Mat multipleMat = null;
  2447. int index = 0;
  2448. for (index = 0; index < this.phaseModels.Count; index++)
  2449. {
  2450. PhaseModel item = this.phaseModels[index];
  2451. if (item.choise && item.mat != null)
  2452. {
  2453. multipleMat = item.mat;
  2454. break;
  2455. }
  2456. }
  2457. if (multipleMat == null)
  2458. return null;
  2459. Bitmap newBit = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(multipleMat);
  2460. Graphics graphics = Graphics.FromImage(newBit);
  2461. for (int i = index + 1; i < this.phaseModels.Count; i++)
  2462. {
  2463. PhaseModel item = this.phaseModels[i];
  2464. if (item.choise && item.mat != null)
  2465. {
  2466. OpenCvSharp.Mat targetMat = item.mat;
  2467. graphics.DrawImage(OpenCvSharp.Extensions.BitmapConverter.ToBitmap(targetMat), 0, 0, targetMat.Width, targetMat.Height);
  2468. }
  2469. }
  2470. PhaseModel phaseModel = new PhaseModel();
  2471. phaseModel.name = this.PhaseModels[0].name;
  2472. phaseModel.position = this.PhaseModels[0].position;
  2473. phaseModel.color = this.PhaseModels[0].color;
  2474. phaseModel.mat = OpenCvSharp.Extensions.BitmapConverter.ToMat(newBit);
  2475. phaseModel.choise = true;
  2476. phaseModel.rgborhls = this.PhaseModels[0].rgborhls;
  2477. phaseModel.minB = this.PhaseModels[0].minB;
  2478. phaseModel.maxB = this.PhaseModels[0].maxB;
  2479. phaseModel.minG = this.PhaseModels[0].minG;
  2480. phaseModel.maxG = this.PhaseModels[0].maxG;
  2481. phaseModel.minR = this.PhaseModels[0].minR;
  2482. phaseModel.maxR = this.PhaseModels[0].maxR;
  2483. phaseModel.minH = this.PhaseModels[0].minH;
  2484. phaseModel.maxH = this.PhaseModels[0].maxH;
  2485. phaseModel.minL = this.PhaseModels[0].minL;
  2486. phaseModel.maxL = this.PhaseModels[0].maxL;
  2487. phaseModel.minS = this.PhaseModels[0].minS;
  2488. phaseModel.maxS = this.PhaseModels[0].maxS;
  2489. return phaseModel;
  2490. }
  2491. }
  2492. }
  2493. public List<PhaseModel> PhaseModelsForCopy
  2494. {
  2495. get
  2496. {
  2497. List<PhaseModel> list = new List<PhaseModel>();
  2498. if (this.phaseModels != null && this.phaseModels.Count > 0)
  2499. {
  2500. foreach (PhaseModel phase in this.phaseModels)
  2501. {
  2502. if (phase.mat == null)//跳过null的避免报错
  2503. continue;
  2504. PhaseModel phase1 = new PhaseModel();
  2505. phase1.choise = phase.choise;
  2506. phase1.color = phase.color;
  2507. phase1.mat = new OpenCvSharp.Mat();
  2508. phase.mat.CopyTo(phase1.mat);
  2509. phase1.name = phase.name;
  2510. phase1.position = phase.position;
  2511. list.Add(phase1);
  2512. }
  2513. }
  2514. return list;
  2515. }
  2516. }
  2517. #endregion
  2518. #region DocimentView的子控件的事件
  2519. private void Panel_KeyPress(object sender, KeyPressEventArgs e)
  2520. {
  2521. OnDocumentKeyPress(e);
  2522. }
  2523. private void Panel_LostFocus(object sender, EventArgs e)
  2524. {
  2525. this.raiseFirstInputAfterGotFocus = false;
  2526. }
  2527. private void Panel_GotFocus(object sender, EventArgs e)
  2528. {
  2529. this.raiseFirstInputAfterGotFocus = true;
  2530. }
  2531. private void Panel_KeyDown(object sender, KeyEventArgs e)
  2532. {
  2533. if (e.KeyValue == 17)
  2534. {
  2535. this.controlPress = true;
  2536. }
  2537. CheckForFirstInputAfterGotFocus();
  2538. OnDocumentKeyDown(e);
  2539. if (!e.Handled)
  2540. {
  2541. PointF oldPt = this.DocumentScrollPositionF;
  2542. PointF newPt = oldPt;
  2543. RectangleF vdr = VisibleDocumentRectangleF;
  2544. switch (e.KeyData)
  2545. {
  2546. case Keys.Next:
  2547. newPt.Y += vdr.Height;
  2548. break;
  2549. case (Keys.Next | Keys.Shift):
  2550. newPt.X += vdr.Width;
  2551. break;
  2552. case Keys.Prior:
  2553. newPt.Y -= vdr.Height;
  2554. break;
  2555. case (Keys.Prior | Keys.Shift):
  2556. newPt.X -= vdr.Width;
  2557. break;
  2558. case Keys.Home:
  2559. if (oldPt.X == 0)
  2560. {
  2561. newPt.Y = 0;
  2562. }
  2563. else
  2564. {
  2565. newPt.X = 0;
  2566. }
  2567. break;
  2568. case Keys.End:
  2569. if (vdr.Right < this.document.Width - 1)
  2570. {
  2571. newPt.X = this.document.Width;
  2572. }
  2573. else
  2574. {
  2575. newPt.Y = this.document.Height;
  2576. }
  2577. break;
  2578. default:
  2579. break;
  2580. }
  2581. if (newPt != oldPt)
  2582. {
  2583. DocumentScrollPositionF = newPt;
  2584. e.Handled = true;
  2585. }
  2586. }
  2587. }
  2588. private void Panel_Scroll(object sender, System.Windows.Forms.ScrollEventArgs e)
  2589. {
  2590. OnScroll(e);
  2591. UpdateRulerOffsets();
  2592. }
  2593. private void Panel_KeyUp(object sender, KeyEventArgs e)
  2594. {
  2595. this.controlPress = false;
  2596. OnDocumentKeyUp(e);
  2597. }
  2598. /// <summary>
  2599. /// 鼠标按下事件
  2600. /// </summary>
  2601. /// <param name="sender"></param>
  2602. /// <param name="e"></param>
  2603. protected virtual void MouseEvent_Down(object sender, MouseEventArgs e)
  2604. {
  2605. if (tools != null)
  2606. {
  2607. //tools[activeTool].OnMouseDown(this, e);
  2608. tools[_activeTool].InvokeMember("OnMouseDown",
  2609. BindingFlags.Public |
  2610. BindingFlags.Static |
  2611. BindingFlags.InvokeMethod,
  2612. null,
  2613. null,
  2614. new object[2] { this, e });
  2615. }
  2616. if (this.appWorkspace != null)
  2617. {
  2618. this.appWorkspace.SetDrawNodes();
  2619. // this.appWorkspace.RefreshCameraPriview();
  2620. }
  2621. }
  2622. [DllImport("user32.dll", EntryPoint = "mouse_event", SetLastError = true)]
  2623. private static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int a);
  2624. public void MouseEvent_Del(object sender, MouseEventArgs e)
  2625. {
  2626. try
  2627. {
  2628. tools[_activeTool].InvokeMember("beginWithNewObject",
  2629. BindingFlags.Public |
  2630. BindingFlags.Static |
  2631. BindingFlags.InvokeMethod,
  2632. null,
  2633. null,
  2634. new object[0] { });
  2635. tools[0].InvokeMember("OnDelKeyDown",
  2636. BindingFlags.Public |
  2637. BindingFlags.Static |
  2638. BindingFlags.InvokeMethod,
  2639. null,
  2640. null,
  2641. new object[2] { this, e });
  2642. }
  2643. catch
  2644. {
  2645. }
  2646. if (this.appWorkspace != null)
  2647. {
  2648. this.appWorkspace.RefreshLabelListDialog();
  2649. this.appWorkspace.RefreshMeasureListView();
  2650. }
  2651. }
  2652. /// <summary>
  2653. /// 鼠标移动事件
  2654. /// </summary>
  2655. /// <param name="sender"></param>
  2656. /// <param name="e"></param>
  2657. protected virtual void MouseEvent_Move(object sender, MouseEventArgs e)
  2658. {
  2659. //
  2660. // 是否绘制辅助线
  2661. //
  2662. if (this.auxiliaryLineEnabled)
  2663. {
  2664. path = new GraphicsPath();
  2665. path.AddLine(new Point(0, e.Y), new Point(Width, e.Y));
  2666. path1 = new GraphicsPath();
  2667. path1.AddLine(new Point(e.X, 0), new Point(e.X, Height));
  2668. this.Refresh();
  2669. }
  2670. //
  2671. // 处理标注测量
  2672. //
  2673. if (!initialized)
  2674. return;
  2675. if (e.Button == MouseButtons.Left || e.Button == MouseButtons.None)
  2676. if (tools != null)
  2677. {
  2678. tools[_activeTool].InvokeMember("OnMouseMove",
  2679. BindingFlags.Public |
  2680. BindingFlags.Static |
  2681. BindingFlags.InvokeMethod,
  2682. null,
  2683. null,
  2684. new object[2] { this, e });
  2685. }
  2686. else
  2687. this.Cursor = Cursors.Default;
  2688. }
  2689. /// <summary>
  2690. /// 鼠标抬起事件
  2691. /// </summary>
  2692. /// <param name="sender"></param>
  2693. /// <param name="e"></param>
  2694. private void MouseEvent_Up(object sender, MouseEventArgs e)
  2695. {
  2696. if (tools != null)
  2697. {
  2698. if (e.Button == MouseButtons.Left)
  2699. tools[_activeTool].InvokeMember("OnMouseUp",
  2700. BindingFlags.Public |
  2701. BindingFlags.Static |
  2702. BindingFlags.InvokeMethod,
  2703. null,
  2704. null,
  2705. new object[2] { this, e });
  2706. //tools[activeTool].OnMouseUp(this, e);
  2707. }
  2708. if (this.appWorkspace != null)
  2709. {
  2710. this.appWorkspace.RefreshLabelListDialog();
  2711. this.appWorkspace.RefreshMeasureListView();
  2712. this.appWorkspace.RefreshListView();
  2713. this.appWorkspace.RefreshOpticalDensity();
  2714. }
  2715. }
  2716. /// <summary>
  2717. /// 鼠标单击事件
  2718. /// </summary>
  2719. /// <param name="sender"></param>
  2720. /// <param name="e"></param>
  2721. private void MouseEvent_Click(object sender, MouseEventArgs e)
  2722. {
  2723. if (tools != null)
  2724. {
  2725. tools[_activeTool].InvokeMember("OnMouseClick",
  2726. BindingFlags.Public |
  2727. BindingFlags.Static |
  2728. BindingFlags.InvokeMethod,
  2729. null,
  2730. null,
  2731. new object[2] { this, e });
  2732. //tools[activeTool].OnMouseClick(this, e);
  2733. }
  2734. }
  2735. /// <summary>
  2736. /// 鼠标双击事件
  2737. /// </summary>
  2738. /// <param name="sender"></param>
  2739. /// <param name="e"></param>
  2740. private void MouseEvent_DoubleClick(object sender, MouseEventArgs e)
  2741. {
  2742. if (e.Button == MouseButtons.Left && e.Clicks == 2)
  2743. {
  2744. if (this.controlPress)
  2745. {
  2746. List<DrawObject> list = this.graphicsList.GetDrawToolTypeList(DrawToolType.ImageCut);
  2747. if (list != null && list.Count > 0)
  2748. {
  2749. Point ene = GetScalePoint(e.Location);
  2750. RectangleF rect = list[0].Rectangle;
  2751. if (ene.X > rect.X && ene.X < (rect.X + rect.Width) && ene.Y > rect.Y && ene.Y < (rect.Y + rect.Height))
  2752. {
  2753. this.appWorkspace.CopyAndPasteByControlAndDoubleClick();
  2754. }
  2755. }
  2756. this.controlPress = false;
  2757. }
  2758. else
  2759. {
  2760. if (tools != null)
  2761. {
  2762. tools[_activeTool].InvokeMember("OnMouseLeftDoubleClick",
  2763. BindingFlags.Public |
  2764. BindingFlags.Static |
  2765. BindingFlags.InvokeMethod,
  2766. null,
  2767. null,
  2768. new object[2] { this, e });
  2769. }
  2770. }
  2771. }
  2772. //tools[activeTool].OnMouseLeftDoubleClick(this, e);
  2773. }
  2774. /// <summary>
  2775. /// 获取panel显示内容 生成图片返回
  2776. /// </summary>
  2777. /// <returns></returns>
  2778. public Bitmap panelBitmap(bool nail = false)
  2779. {
  2780. if (compositionSurface == null)
  2781. {
  2782. return null;
  2783. }
  2784. Bitmap origin = compositionSurface.CreateAliasedBitmap();
  2785. Bitmap newBit = null;
  2786. if (!nail)
  2787. {
  2788. newBit = origin;
  2789. }
  2790. else if (origin.Width > origin.Height)
  2791. {
  2792. newBit = Surface.MakeThumbnail(origin, 64, 64, "W");
  2793. }
  2794. else if (origin.Height > origin.Width)
  2795. {
  2796. newBit = Surface.MakeThumbnail(origin, 64, 64, "H");
  2797. }
  2798. else
  2799. {
  2800. newBit = Surface.MakeThumbnail(origin, 64, 64, "W");
  2801. }
  2802. int width = newBit.Width;// (int)(this.compositionSurface.Width * this.scaleFactor.Ratio);
  2803. int height = newBit.Height;// (int)(this.compositionSurface.Height * this.scaleFactor.Ratio);
  2804. int x = 0;// (rc.Width < width) ? this.panel.AutoScrollPosition.X + offsetHalfW : (rc.Width - width) / 2;
  2805. int y = 0;// (rc.Height < height) ? this.panel.AutoScrollPosition.Y + offsetHalfH : (rc.Height - height) / 2;
  2806. Graphics graphics = Graphics.FromImage(newBit);
  2807. //
  2808. // 以下是绘制相
  2809. //
  2810. if (phaseModels.Count > 0)
  2811. {
  2812. foreach (PhaseModel model in phaseModels)
  2813. {
  2814. if (model != null && model.choise && model.mat != null)
  2815. {
  2816. Bitmap map = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(model.mat);
  2817. graphics.DrawImage(map, x, y, width, height);
  2818. }
  2819. }
  2820. }
  2821. //
  2822. // 以下是绘制辅助线
  2823. //
  2824. if (this.auxiliaryLineEnabled)
  2825. {
  2826. if (path != null && path1 != null)
  2827. {
  2828. graphics.DrawPath(new Pen(Color.Red), path);
  2829. graphics.DrawPath(new Pen(Color.Red), path1);
  2830. }
  2831. }
  2832. //
  2833. // 以下是绘制网格、标注、测量、视场等开始
  2834. //
  2835. graphics.TranslateTransform(x, y);
  2836. graphics.ScaleTransform((float)1.0/*this.scaleFactor.Ratio*/, (float)1.0/*this.scaleFactor.Ratio*/);
  2837. //
  2838. // 以下是绘制网格
  2839. //
  2840. if (this.gridLineEnabled && this.AppWorkspaceTop != null)
  2841. {
  2842. double unitLength = 1;
  2843. this.appWorkspace.getMeasureInfo().TryGetValue(MeasurementUnit.Micron, out unitLength);
  2844. int unit = AppWorkspaceTop.GetGridModel().grid.Unit; //单位
  2845. int actualLength = AppWorkspaceTop.GetGridModel().grid.ActualLength;//实际长度
  2846. if (unit == (int)MeasurementUnit.Micron)
  2847. {
  2848. AppWorkspaceTop.GetGridModel().grid.SideLength = (int)((double)actualLength / unitLength);
  2849. }
  2850. //else if (unit == (int)MeasurementUnit.Pixel)
  2851. //{
  2852. // AppWorkspaceTop.GetGridModel().grid.SideLength = actualLength;
  2853. //}
  2854. DrawRulerHelper.drawGrid(AppWorkspaceTop.GetGridModel(), graphics, this.compositionSurface.Width, this.compositionSurface.Height);
  2855. }
  2856. //
  2857. //以下是绘制矩形网格
  2858. //
  2859. if (this.gridRectangleEnabled && this.AppWorkspaceTop != null && micronRatio > 0)
  2860. {
  2861. DrawRulerHelper.drawGridRectangle(AppWorkspaceTop.GetGridModel(), graphics, this.compositionSurface.Width, this.compositionSurface.Height, micronRatio);
  2862. }
  2863. //
  2864. //以下是绘制圆形网格
  2865. //
  2866. if (this.gridRoundEnabled && this.AppWorkspaceTop != null && micronRatio > 0)
  2867. {
  2868. DrawRulerHelper.drawGridRound(AppWorkspaceTop.GetGridModel(), graphics, this.compositionSurface.Width, this.compositionSurface.Height, micronRatio);
  2869. }
  2870. //
  2871. //以下是绘制十字线
  2872. //
  2873. if (this.gridCrossCurveEnabled && this.AppWorkspaceTop != null)
  2874. {
  2875. DrawRulerHelper.drawGridCrossCurve(null, graphics, this.compositionSurface.Width, this.compositionSurface.Height);
  2876. }
  2877. //
  2878. // 以下是绘制标注、测量、视场
  2879. //
  2880. if (graphicsList != null)
  2881. {
  2882. graphicsList.Draw(graphics);
  2883. }
  2884. //
  2885. // 以下是绘制网格(覆盖整个图片)
  2886. //
  2887. if (this.gridLineFullEnabled && this.AppWorkspaceTop != null)
  2888. {
  2889. DrawRulerHelper.drawGridFull(AppWorkspaceTop.GetGridModel(), graphics, this.compositionSurface.Width, this.compositionSurface.Height);
  2890. }
  2891. //
  2892. // 以下是绘制图像(工艺图比照定位点图像绘制)
  2893. //
  2894. if (m_selectedBitmap != null)
  2895. {
  2896. graphics.DrawImage(m_selectedBitmap, new PointF(m_artworkImageRectangle.X, m_artworkImageRectangle.Y));
  2897. }
  2898. return newBit;
  2899. }
  2900. /// <summary>
  2901. /// 获取panel显示内容 生成图片返回
  2902. /// </summary>
  2903. /// <returns></returns>
  2904. public Bitmap NewPanelBitmap(Bitmap newBit)
  2905. {
  2906. int width = newBit.Width;// (int)(this.compositionSurface.Width * this.scaleFactor.Ratio);
  2907. int height = newBit.Height;// (int)(this.compositionSurface.Height * this.scaleFactor.Ratio);
  2908. int x = 0;// (rc.Width < width) ? this.panel.AutoScrollPosition.X + offsetHalfW : (rc.Width - width) / 2;
  2909. int y = 0;// (rc.Height < height) ? this.panel.AutoScrollPosition.Y + offsetHalfH : (rc.Height - height) / 2;
  2910. Graphics graphics = Graphics.FromImage(newBit);
  2911. //
  2912. // 以下是绘制相
  2913. //
  2914. if (phaseModels.Count > 0)
  2915. {
  2916. foreach (PhaseModel model in phaseModels)
  2917. {
  2918. if (model != null && model.choise && model.mat != null)
  2919. {
  2920. Bitmap map = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(model.mat);
  2921. graphics.DrawImage(map, x, y, width, height);
  2922. }
  2923. }
  2924. }
  2925. //
  2926. // 以下是绘制辅助线
  2927. //
  2928. //if (this.auxiliaryLineEnabled)
  2929. //{
  2930. // if (path != null && path1 != null)
  2931. // {
  2932. // graphics.DrawPath(new Pen(Color.Red), path);
  2933. // graphics.DrawPath(new Pen(Color.Red), path1);
  2934. // }
  2935. //}
  2936. //
  2937. // 以下是绘制网格、标注、测量、视场等开始
  2938. //
  2939. graphics.TranslateTransform(x, y);
  2940. graphics.ScaleTransform((float)1.0/*this.scaleFactor.Ratio*/, (float)1.0/*this.scaleFactor.Ratio*/);
  2941. //
  2942. // 以下是绘制网格
  2943. //
  2944. if (this.gridLineEnabled && this.AppWorkspaceTop != null)
  2945. {
  2946. double unitLength = 1;
  2947. this.appWorkspace.getMeasureInfo().TryGetValue(MeasurementUnit.Micron, out unitLength);
  2948. int unit = AppWorkspaceTop.GetGridModel().grid.Unit; //单位
  2949. int actualLength = AppWorkspaceTop.GetGridModel().grid.ActualLength;//实际长度
  2950. if (unit == (int)MeasurementUnit.Micron)
  2951. {
  2952. AppWorkspaceTop.GetGridModel().grid.SideLength = (int)((double)actualLength / unitLength);
  2953. }
  2954. //else if (unit == (int)MeasurementUnit.Pixel)
  2955. //{
  2956. // AppWorkspaceTop.GetGridModel().grid.SideLength = actualLength;
  2957. //}
  2958. DrawRulerHelper.drawGrid(AppWorkspaceTop.GetGridModel(), graphics, this.compositionSurface.Width, this.compositionSurface.Height);
  2959. }
  2960. //
  2961. //以下是绘制矩形网格
  2962. //
  2963. if (this.gridRectangleEnabled && this.AppWorkspaceTop != null && micronRatio > 0)
  2964. {
  2965. DrawRulerHelper.drawGridRectangle(AppWorkspaceTop.GetGridModel(), graphics, this.compositionSurface.Width, this.compositionSurface.Height, micronRatio);
  2966. }
  2967. //
  2968. //以下是绘制圆形网格
  2969. //
  2970. if (this.gridRoundEnabled && this.AppWorkspaceTop != null && micronRatio > 0)
  2971. {
  2972. DrawRulerHelper.drawGridRound(AppWorkspaceTop.GetGridModel(), graphics, this.compositionSurface.Width, this.compositionSurface.Height, micronRatio);
  2973. }
  2974. //
  2975. //以下是绘制十字线
  2976. //
  2977. if (this.gridCrossCurveEnabled && this.AppWorkspaceTop != null)
  2978. {
  2979. DrawRulerHelper.drawGridCrossCurve(null, graphics, this.compositionSurface.Width, this.compositionSurface.Height);
  2980. }
  2981. //
  2982. // 以下是绘制标注、测量、视场
  2983. //
  2984. if (graphicsList != null)
  2985. {
  2986. graphicsList.Draw(graphics);
  2987. }
  2988. //
  2989. // 以下是绘制网格(覆盖整个图片)
  2990. //
  2991. if (this.gridLineFullEnabled && this.AppWorkspaceTop != null)
  2992. {
  2993. DrawRulerHelper.drawGridFull(AppWorkspaceTop.GetGridModel(), graphics, this.compositionSurface.Width, this.compositionSurface.Height);
  2994. }
  2995. //
  2996. // 以下是绘制图像(工艺图比照定位点图像绘制)
  2997. //
  2998. if (m_selectedBitmap != null)
  2999. {
  3000. graphics.DrawImage(m_selectedBitmap, new PointF(m_artworkImageRectangle.X, m_artworkImageRectangle.Y));
  3001. }
  3002. return newBit;
  3003. }
  3004. /// <summary>
  3005. /// 获取panel标注、测量、视场显示内容 生成图片返回
  3006. /// </summary>
  3007. /// <returns></returns>
  3008. public Bitmap FieldBitmap(Bitmap newBit)
  3009. {
  3010. int width = newBit.Width;// (int)(this.compositionSurface.Width * this.scaleFactor.Ratio);
  3011. int height = newBit.Height;// (int)(this.compositionSurface.Height * this.scaleFactor.Ratio);
  3012. int x = 0;// (rc.Width < width) ? this.panel.AutoScrollPosition.X + offsetHalfW : (rc.Width - width) / 2;
  3013. int y = 0;// (rc.Height < height) ? this.panel.AutoScrollPosition.Y + offsetHalfH : (rc.Height - height) / 2;
  3014. Graphics graphics = Graphics.FromImage(newBit);
  3015. //
  3016. // 以下是绘制网格、标注、测量、视场等开始
  3017. //
  3018. graphics.TranslateTransform(x, y);
  3019. graphics.ScaleTransform((float)1.0/*this.scaleFactor.Ratio*/, (float)1.0/*this.scaleFactor.Ratio*/);
  3020. //
  3021. // 以下是绘制标注、测量、视场
  3022. //
  3023. if (graphicsList != null)
  3024. {
  3025. graphicsList.Draw(graphics);
  3026. }
  3027. return newBit;
  3028. }
  3029. /// <summary>
  3030. /// 样式信息
  3031. /// </summary>
  3032. /// <summary>
  3033. /// 绘制事件
  3034. /// </summary>
  3035. /// <param name="sender"></param>
  3036. /// <param name="e"></param>
  3037. private unsafe void panelPaint(object sender, PaintEventArgs e)
  3038. {
  3039. if (compositionSurface != null)
  3040. {
  3041. // 以下是计算绘制图片的位置和大小并绘制图片
  3042. Rectangle rc = this.panel.ClientRectangle;
  3043. int width = (int)(this.compositionSurface.Width * this.scaleFactor.Ratio);
  3044. int height = (int)(this.compositionSurface.Height * this.scaleFactor.Ratio);
  3045. int x = (rc.Width < width) ? this.panel.AutoScrollPosition.X + offsetHalfW : (rc.Width - width) / 2;
  3046. int y = (rc.Height < height) ? this.panel.AutoScrollPosition.Y + offsetHalfH : (rc.Height - height) / 2;
  3047. //第四个版本,只绘制当前需要显示的大小的图片
  3048. //if (this.compositionSurface.Bitmap != null)
  3049. if (this.compositionSurface.CreateAliasedBitmap() != null)
  3050. {
  3051. using (Surface doubleBuffer = GetDoubleBuffer(new Size((rc.Width < width) ? rc.Width : width, (rc.Height < height) ? rc.Height : height)))
  3052. {
  3053. try
  3054. {
  3055. using (RenderArgs renderArgs = new RenderArgs(doubleBuffer))
  3056. {
  3057. DrawArea(renderArgs, new Point((rc.Width < width) ? -x : 0, (rc.Height < height) ? -y : 0));
  3058. if (m_center)
  3059. {
  3060. e.Graphics.DrawImage(doubleBuffer.CreateAliasedBitmap(),
  3061. (rc.Width < width) ? 0 : (rc.Width - width) / 2,
  3062. (rc.Height < height) ? 0 : (rc.Height - height) / 2,
  3063. (rc.Width < width) ? rc.Width : width, (rc.Height < height) ? rc.Height : height);
  3064. }
  3065. else
  3066. {
  3067. e.Graphics.DrawImage(doubleBuffer.CreateAliasedBitmap(),
  3068. m_pLeft,
  3069. m_pTop,
  3070. (rc.Width < width) ? rc.Width : width, (rc.Height < height) ? rc.Height : height);
  3071. }
  3072. }
  3073. }
  3074. catch { }
  3075. }
  3076. }
  3077. //
  3078. // 绘制选择的矩形
  3079. //
  3080. if (drawRectangle)
  3081. {
  3082. /*Pen pen = new Pen(Color.Black);
  3083. pen.DashStyle = DashStyle.Dot;*/
  3084. e.Graphics.DrawRectangle(new Pen(Color.Black), this.DrawRectangle);
  3085. }
  3086. if (phaseModels.Count > 0)
  3087. {
  3088. int TopIndex = -1;
  3089. foreach (PhaseModel model in phaseModels)
  3090. {
  3091. TopIndex++;
  3092. if (model == null || model.mat == null || model.choise==false) continue;
  3093. using (Surface doubleBufferA = GetDoubleBuffer(new Size((rc.Width < width) ? rc.Width : width, (rc.Height < height) ? rc.Height : height)))
  3094. {
  3095. try
  3096. {
  3097. using (RenderArgs renderArgs = new RenderArgs(doubleBufferA))
  3098. {
  3099. DrawArea1(renderArgs, new Point((rc.Width < width) ? -x : 0, (rc.Height < height) ? -y : 0), TopIndex);
  3100. if (m_center)
  3101. {
  3102. e.Graphics.DrawImage(doubleBufferA.CreateAliasedBitmap(),
  3103. (rc.Width < width) ? 0 : (rc.Width - width) / 2,
  3104. (rc.Height < height) ? 0 : (rc.Height - height) / 2,
  3105. (rc.Width < width) ? rc.Width : width, (rc.Height < height) ? rc.Height : height);
  3106. }
  3107. else
  3108. {
  3109. e.Graphics.DrawImage(doubleBufferA.CreateAliasedBitmap(),
  3110. m_pLeft,
  3111. m_pTop,
  3112. (rc.Width < width) ? rc.Width : width, (rc.Height < height) ? rc.Height : height);
  3113. }
  3114. }
  3115. }
  3116. catch { }
  3117. }
  3118. }
  3119. }
  3120. //
  3121. // 以下是绘制相
  3122. //
  3123. /*if (phaseModels.Count > 0)
  3124. {
  3125. foreach (PhaseModel model in phaseModels)
  3126. {
  3127. if (model != null && model.choise && model.mat != null && !model.mat.IsDisposed)
  3128. {
  3129. Bitmap map = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(model.mat);
  3130. e.Graphics.DrawImage(map, x, y, width, height);
  3131. }
  3132. }
  3133. }*/
  3134. //
  3135. // 以下是绘制辅助线
  3136. //
  3137. if (this.auxiliaryLineEnabled)
  3138. {
  3139. if (path != null && path1 != null)
  3140. {
  3141. Pen linePen = new Pen(Color.FromArgb(GetGuideStyleModel().lineColour), (float)GetGuideStyleModel().lineWidth);
  3142. linePen.DashStyle = (DashStyle)GetGuideStyleModel().lineStyle;
  3143. e.Graphics.DrawPath(linePen, path);
  3144. e.Graphics.DrawPath(linePen, path1);
  3145. }
  3146. }
  3147. //
  3148. // 以下是绘制网格、标注、测量、视场等开始
  3149. //
  3150. e.Graphics.TranslateTransform(x, y);
  3151. e.Graphics.ScaleTransform((float)this.scaleFactor.Ratio, (float)this.scaleFactor.Ratio);
  3152. //
  3153. // 以下是绘制网格
  3154. //
  3155. if (this.gridLineEnabled && this.AppWorkspaceTop != null)
  3156. {
  3157. //double unitLength = 1;
  3158. //this.appWorkspace.getMeasureInfo().TryGetValue(MeasurementUnit.Micron, out unitLength);
  3159. double unitLength = this.GetRuler(MeasurementUnit.Micron); //获取标尺
  3160. int unit = AppWorkspaceTop.GetGridModel().grid.Unit; //单位
  3161. int actualLength = AppWorkspaceTop.GetGridModel().grid.ActualLength; //实际宽度
  3162. int actualHeight = AppWorkspaceTop.GetGridModel().grid.ActualHeight; //实际高度
  3163. if (unit == (int)MeasurementUnit.Micron)
  3164. {
  3165. AppWorkspaceTop.GetGridModel().grid.SideLength = (int)((double)actualLength / unitLength);
  3166. AppWorkspaceTop.GetGridModel().grid.SideHeight = (int)((double)actualHeight / unitLength);
  3167. }
  3168. //else if (unit == (int)MeasurementUnit.Pixel)
  3169. //{
  3170. // AppWorkspaceTop.GetGridModel().grid.SideLength = actualLength;
  3171. //}
  3172. DrawRulerHelper.drawGrid(AppWorkspaceTop.GetGridModel(), e.Graphics, this.compositionSurface.Width, this.compositionSurface.Height);
  3173. }
  3174. //
  3175. //以下是绘制矩形网格
  3176. //
  3177. if (this.gridRectangleEnabled && this.AppWorkspaceTop != null && micronRatio > 0)
  3178. {
  3179. double unitLength = this.GetRuler(MeasurementUnit.Micron); //获取标尺
  3180. int unit = AppWorkspaceTop.GetGridModel().grid.Unit; //单位
  3181. int actualLength = AppWorkspaceTop.GetGridModel().rectangle.ActualLength; //实际边长
  3182. if (unit == (int)MeasurementUnit.Micron)
  3183. {
  3184. AppWorkspaceTop.GetGridModel().rectangle.sideLength = (int)((double)actualLength / unitLength);
  3185. }
  3186. DrawRulerHelper.drawGridRectangle(AppWorkspaceTop.GetGridModel(), e.Graphics, this.compositionSurface.Width, this.compositionSurface.Height, micronRatio);
  3187. }
  3188. //
  3189. //以下是绘制圆形网格
  3190. //
  3191. if (this.gridRoundEnabled && this.AppWorkspaceTop != null && micronRatio > 0)
  3192. {
  3193. double unitLength = this.GetRuler(MeasurementUnit.Micron); //获取标尺
  3194. int unit = AppWorkspaceTop.GetGridModel().grid.Unit; //单位
  3195. int actualDiameter = AppWorkspaceTop.GetGridModel().round.ActualDiameter; //实际直径
  3196. if (unit == (int)MeasurementUnit.Micron)
  3197. {
  3198. AppWorkspaceTop.GetGridModel().round.diameter = (int)((double)actualDiameter / unitLength);
  3199. }
  3200. DrawRulerHelper.drawGridRound(AppWorkspaceTop.GetGridModel(), e.Graphics, this.compositionSurface.Width, this.compositionSurface.Height, micronRatio);
  3201. }
  3202. //
  3203. //以下是绘制十字线
  3204. //
  3205. if (this.gridCrossCurveEnabled && this.AppWorkspaceTop != null)
  3206. {
  3207. DrawRulerHelper.drawGridCrossCurve(null, e.Graphics, this.compositionSurface.Width, this.compositionSurface.Height);
  3208. }
  3209. //
  3210. // 以下是绘制标注、测量、视场
  3211. //
  3212. if (graphicsList != null)
  3213. {
  3214. graphicsList.Draw(e.Graphics);
  3215. }
  3216. //
  3217. // 以下是绘制网格(覆盖整个图片)
  3218. //
  3219. if (this.gridLineFullEnabled && this.AppWorkspaceTop != null)
  3220. {
  3221. DrawRulerHelper.drawGridFull(AppWorkspaceTop.GetGridModel(), e.Graphics, this.compositionSurface.Width, this.compositionSurface.Height);
  3222. }
  3223. //
  3224. // 以下是绘制图像(工艺图比照定位点图像绘制)
  3225. //
  3226. if (m_selectedBitmap != null)
  3227. {
  3228. e.Graphics.DrawImage(m_selectedBitmap, new PointF(m_artworkImageRectangle.X, m_artworkImageRectangle.Y));
  3229. }
  3230. e.Graphics.ScaleTransform(1 / (float)this.scaleFactor.Ratio, 1 / (float)this.scaleFactor.Ratio);
  3231. e.Graphics.TranslateTransform(-x, -y);
  3232. }
  3233. }
  3234. #endregion
  3235. /// <summary>
  3236. /// 获取panel可见位置的图片
  3237. /// </summary>
  3238. /// <returns></returns>
  3239. public Bitmap GetClientRangePic()
  3240. {
  3241. Rectangle rc = this.panel.ClientRectangle;
  3242. int width = (int)(this.compositionSurface.Width * this.scaleFactor.Ratio);
  3243. int height = (int)(this.compositionSurface.Height * this.scaleFactor.Ratio);
  3244. int x = (rc.Width < width) ? this.panel.AutoScrollPosition.X + offsetHalfW : (rc.Width - width) / 2;
  3245. int y = (rc.Height < height) ? this.panel.AutoScrollPosition.Y + offsetHalfH : (rc.Height - height) / 2;
  3246. if (this.compositionSurface.CreateAliasedBitmap() != null)
  3247. {
  3248. using (Surface doubleBuffer = GetDoubleBuffer(new Size((rc.Width < width) ? rc.Width : width, (rc.Height < height) ? rc.Height : height)))
  3249. {
  3250. using (RenderArgs renderArgs = new RenderArgs(doubleBuffer))
  3251. {
  3252. DrawArea(renderArgs, new Point((rc.Width < width) ? -x : 0, (rc.Height < height) ? -y : 0));
  3253. return doubleBuffer.CreateAliasedBitmap();
  3254. }
  3255. }
  3256. }
  3257. return this.compositionSurface.CreateAliasedBitmap();
  3258. /*IntPtr handle = this.panel.Handle;
  3259. IntPtr hdcSrc = SafeNativeMethods.GetWindowDC(handle);
  3260. SafeNativeMethods.RECT windowRect = new SafeNativeMethods.RECT();
  3261. SafeNativeMethods.GetWindowRect(handle, ref windowRect);
  3262. int width = windowRect.right - windowRect.left - ((this.panel.VerticalScroll.Visible) ? 18 : 0);
  3263. int height = windowRect.bottom - windowRect.top - ((this.panel.HorizontalScroll.Visible) ? 18 : 0);
  3264. IntPtr hdcDest = SafeNativeMethods.CreateCompatibleDC(hdcSrc);
  3265. IntPtr hBitmap = SafeNativeMethods.CreateCompatibleBitmap(hdcSrc, width, height);
  3266. IntPtr hOld = SafeNativeMethods.SelectObject(hdcDest, hBitmap);
  3267. SafeNativeMethods.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, SafeNativeMethods.SRCCOPY);
  3268. SafeNativeMethods.SelectObject(hdcDest, hOld);
  3269. SafeNativeMethods.DeleteDC(hdcDest);
  3270. SafeNativeMethods.ReleaseDC(handle, hdcSrc);
  3271. Bitmap img = Image.FromHbitmap(hBitmap);
  3272. SafeNativeMethods.DeleteObject(hBitmap);
  3273. return img;*/
  3274. }
  3275. #region DocumentView的事件
  3276. protected override void OnLayout(LayoutEventArgs e)
  3277. {
  3278. DoLayout();
  3279. base.OnLayout(e);
  3280. }
  3281. /// <summary>
  3282. /// 让画布获取焦点
  3283. /// </summary>
  3284. public new void Focus()
  3285. {
  3286. this.panel.Focus();
  3287. }
  3288. protected override void OnResize(EventArgs e)
  3289. {
  3290. // enable or disable timer: no sense drawing selection if we're minimized
  3291. Form parentForm = ParentForm;
  3292. if (parentForm != null)
  3293. {
  3294. if (parentForm.WindowState != this.oldWindowState)
  3295. {
  3296. PerformLayout();
  3297. }
  3298. this.oldWindowState = parentForm.WindowState;
  3299. }
  3300. base.OnResize(e);
  3301. DoLayout();
  3302. }
  3303. #endregion
  3304. #region 坐标转换相关方法
  3305. public PointF MouseToDocumentF(Control sender, Point mouse)
  3306. {
  3307. Point screenPoint = sender.PointToScreen(mouse);
  3308. Point sbClient = this.panel.PointToClient(screenPoint);
  3309. PointF docPoint = this.panel.ClientToSurface(new PointF(sbClient.X, sbClient.Y));
  3310. return docPoint;
  3311. }
  3312. public Point MouseToDocument(Control sender, Point mouse)
  3313. {
  3314. Point screenPoint = sender.PointToScreen(mouse);
  3315. Point sbClient = this.panel.PointToClient(screenPoint);
  3316. Point docPoint = Point.Truncate(this.panel.ClientToSurface(sbClient));
  3317. return docPoint;
  3318. }
  3319. /// <summary>
  3320. /// Converts a point from the Windows Forms "client" coordinate space (wrt the DocumentView)
  3321. /// into the Document coordinate space.
  3322. /// </summary>
  3323. /// <param name="clientPt">A Point that is in our client coordinates.</param>
  3324. /// <returns>A Point that is in Document coordinates.</returns>
  3325. public PointF ClientToDocument(Point clientPt)
  3326. {
  3327. Point screen = PointToScreen(clientPt);
  3328. Point sbClient = this.panel.PointToClient(screen);
  3329. return this.panel.ClientToSurface(sbClient);
  3330. }
  3331. /// <summary>
  3332. /// Converts a point from screen coordinates to document coordinates
  3333. /// </summary>
  3334. /// <param name="screen">The point in screen coordinates to convert to document coordinates</param>
  3335. public PointF ScreenToDocument(PointF screen)
  3336. {
  3337. Point offset = this.panel.PointToClient(new Point(0, 0));
  3338. return this.panel.ClientToSurface(new PointF(screen.X + (float)offset.X, screen.Y + (float)offset.Y));
  3339. }
  3340. /// <summary>
  3341. /// Converts a point from screen coordinates to document coordinates
  3342. /// </summary>
  3343. /// <param name="screen">The point in screen coordinates to convert to document coordinates</param>
  3344. public Point ScreenToDocument(Point screen)
  3345. {
  3346. Point offset = this.panel.PointToClient(new Point(0, 0));
  3347. return this.panel.ClientToSurface(new Point(screen.X + offset.X, screen.Y + offset.Y));
  3348. }
  3349. /// <summary>
  3350. /// Converts a PointF from the RealTimeStylus coordinate space
  3351. /// into the Document coordinate space.
  3352. /// </summary>
  3353. /// <param name="clientPt">A Point that is in RealTimeStylus coordinate space.</param>
  3354. /// <returns>A Point that is in Document coordinates.</returns>
  3355. public PointF ClientToSurface(PointF clientPt)
  3356. {
  3357. return this.panel.ClientToSurface(clientPt);
  3358. }
  3359. /// <summary>
  3360. /// Converts a point from Document coordinate space into the Windows Forms "client"
  3361. /// coordinate space.
  3362. /// </summary>
  3363. /// <param name="clientPt">A Point that is in Document coordinates.</param>
  3364. /// <returns>A Point that is in client coordinates.</returns>
  3365. public PointF DocumentToClient(PointF documentPt)
  3366. {
  3367. PointF sbClient = this.panel.SurfaceToClient(documentPt);
  3368. Point screen = this.panel.PointToScreen(Point.Round(sbClient));
  3369. return PointToClient(screen);
  3370. }
  3371. /// <summary>
  3372. /// Converts a rectangle from the Windows Forms "client" coordinate space into the Document
  3373. /// coordinate space.
  3374. /// </summary>
  3375. /// <param name="clientPt">A Rectangle that is in client coordinates.</param>
  3376. /// <returns>A Rectangle that is in Document coordinates.</returns>
  3377. public RectangleF ClientToDocument(Rectangle clientRect)
  3378. {
  3379. Rectangle screen = RectangleToScreen(clientRect);
  3380. screen.X = screen.X + offsetW;
  3381. screen.Y = screen.Y + offsetH;
  3382. Rectangle sbClient = this.panel.RectangleToClient(screen);
  3383. return this.panel.ClientToSurface((RectangleF)sbClient);
  3384. }
  3385. /// <summary>
  3386. /// Converts a rectangle from Document coordinate space into the Windows Forms "client"
  3387. /// coordinate space.
  3388. /// </summary>
  3389. /// <param name="clientPt">A Rectangle that is in Document coordinates.</param>
  3390. /// <returns>A Rectangle that is in client coordinates.</returns>
  3391. public RectangleF DocumentToClient(RectangleF documentRect)
  3392. {
  3393. RectangleF sbClient = this.panel.SurfaceToClient(documentRect);
  3394. Rectangle screen = this.panel.RectangleToScreen(Utility.RoundRectangle(sbClient));
  3395. return RectangleToClient(screen);
  3396. }
  3397. public Rectangle SurfaceToClient(Rectangle surfaceRect)
  3398. {
  3399. return new Rectangle(SurfaceToClient(surfaceRect.Location), SurfaceToClient(surfaceRect.Size));
  3400. }
  3401. public Point SurfaceToClient(Point surfacePt)
  3402. {
  3403. return ScaleFactor.ScalePoint(surfacePt);
  3404. }
  3405. public Size SurfaceToClient(Size surfaceSize)
  3406. {
  3407. return Size.Round(SurfaceToClient((SizeF)surfaceSize));
  3408. }
  3409. public SizeF SurfaceToClient(SizeF surfaceSize)
  3410. {
  3411. return ScaleFactor.ScaleSize(surfaceSize);
  3412. }
  3413. #endregion
  3414. #region 接口实现
  3415. public void IsDirty(GraphicsList gList)
  3416. {
  3417. this.AdjustRendering();
  3418. }
  3419. public RectangleF GetVisibleDocumentRectangleF()
  3420. {
  3421. return this.VisibleDocumentRectangleF;
  3422. //return this.AppWorkspaceTop.GetVisibleDocumentRectangleF();
  3423. }
  3424. public SizeF GetDocumentSize()
  3425. {
  3426. if (this.Document != null)
  3427. return this.Document.Size;
  3428. else
  3429. return new SizeF(0, 0);
  3430. //return this.AppWorkspaceTop.GetDocumentSize();
  3431. }
  3432. /// <summary>
  3433. /// 添加到标注测量历史
  3434. /// </summary>
  3435. /// <param name="c"></param>
  3436. public void AddCommandToHistory(Command c)
  3437. {
  3438. this.undoManager.AddCommandToHistory(c);
  3439. }
  3440. /// <summary>
  3441. /// 初始化标注测量管理
  3442. /// </summary>
  3443. public void ResetUndoManager()
  3444. {
  3445. this.undoManager = new UndoManager(this.GraphicsList);
  3446. }
  3447. /// <summary>
  3448. /// 标注连续绘制标记
  3449. /// </summary>
  3450. /// <returns></returns>
  3451. bool ISurfaceBox.ContinuousDrawingLabel()
  3452. {
  3453. return ContinuousDrawingLabel;
  3454. }
  3455. /// <summary>
  3456. /// 测量连续绘制标记
  3457. /// </summary>
  3458. /// <returns></returns>
  3459. bool ISurfaceBox.ContinuousDrawingMeasure()
  3460. {
  3461. return ContinuousDrawingMeasure;
  3462. }
  3463. /// <summary>
  3464. /// 实际大小标记
  3465. /// </summary>
  3466. /// <returns></returns>
  3467. bool ISurfaceBox.ActualSize()
  3468. {
  3469. return ActualSize;
  3470. }
  3471. /// <summary>
  3472. /// 合适大小标记
  3473. /// </summary>
  3474. /// <returns></returns>
  3475. bool ISurfaceBox.SuitableSize()
  3476. {
  3477. return SuitableSize;
  3478. }
  3479. /// <summary>
  3480. /// 合适高度标记
  3481. /// </summary>
  3482. /// <returns></returns>
  3483. bool ISurfaceBox.SuitableHeight()
  3484. {
  3485. return SuitableHeight;
  3486. }
  3487. /// <summary>
  3488. /// 合适大小宽度标记
  3489. /// </summary>
  3490. /// <returns></returns>
  3491. bool ISurfaceBox.SuitableWidth()
  3492. {
  3493. return SuitableWidth;
  3494. }
  3495. /// <summary>
  3496. /// 图片扩缩
  3497. /// </summary>
  3498. /// <returns></returns>
  3499. bool ISurfaceBox.LockZoom()
  3500. {
  3501. return LockZoom;
  3502. }
  3503. /// <summary>
  3504. /// 定倍显示
  3505. /// </summary>
  3506. /// <returns></returns>
  3507. bool ISurfaceBox.FixedMultiple()
  3508. {
  3509. return FixedMultiple;
  3510. }
  3511. /// <summary>
  3512. /// 合并视场
  3513. /// </summary>
  3514. /// <returns></returns>
  3515. bool ISurfaceBox.MergeFieldOfView()
  3516. {
  3517. return MergeFieldOfView;
  3518. }
  3519. /// <summary>
  3520. /// 删除视场
  3521. /// </summary>
  3522. /// <returns></returns>
  3523. bool ISurfaceBox.DeleteFieldOfView()
  3524. {
  3525. return DeleteFieldOfView;
  3526. }
  3527. /// <summary>
  3528. /// 二值操作连续绘制标记
  3529. /// </summary>
  3530. /// <returns></returns>
  3531. bool ISurfaceBox.ContinuousBinaryAction()
  3532. {
  3533. return ContinuousBinaryAction;
  3534. }
  3535. public PointF ClientToSurface(Point sbClient)
  3536. {
  3537. return ScaleFactor.UnscalePoint(sbClient);
  3538. //return this.ClientToSurface(sbClient);
  3539. }
  3540. public void SetDirty()
  3541. {
  3542. this.Document.Dirty = true;
  3543. }
  3544. /// <summary>
  3545. /// 获取原始坐标点
  3546. /// </summary>
  3547. /// <returns></returns>
  3548. public Point GetCalcOriginPoint()
  3549. {
  3550. Rectangle rc = this.panel.ClientRectangle;
  3551. int width = (int)(this.compositionSurface.Width * this.scaleFactor.Ratio);
  3552. int height = (int)(this.compositionSurface.Height * this.scaleFactor.Ratio);
  3553. int x = (rc.Width < width) ? this.panel.AutoScrollPosition.X + offsetHalfW : (rc.Width - width) / 2;
  3554. int y = (rc.Height < height) ? this.panel.AutoScrollPosition.Y + offsetHalfH : (rc.Height - height) / 2;
  3555. return new Point(x, y);
  3556. }
  3557. /// <summary>
  3558. /// 获取未缩放的坐标点
  3559. /// </summary>
  3560. /// <param name="point"></param>
  3561. /// <returns></returns>
  3562. public Point GetScalePoint(PointF point)
  3563. {
  3564. Rectangle rc = this.panel.ClientRectangle;
  3565. int width = this.compositionSurface == null ? 800 : (int)(this.compositionSurface.Width * this.scaleFactor.Ratio);
  3566. int height = this.compositionSurface == null ? 600 : (int)(this.compositionSurface.Height * this.scaleFactor.Ratio);
  3567. int x = (rc.Width < width) ? this.panel.AutoScrollPosition.X + offsetHalfW : (rc.Width - width) / 2;
  3568. int y = (rc.Height < height) ? this.panel.AutoScrollPosition.Y + offsetHalfH : (rc.Height - height) / 2;
  3569. return new Point(
  3570. (int)((point.X - x) / this.scaleFactor.Ratio),
  3571. (int)((point.Y - y) / this.scaleFactor.Ratio)
  3572. );
  3573. //return this.scaleFactor.UnscalePoint(location);
  3574. }
  3575. public int UnscaleScalar(int deltaX)
  3576. {
  3577. return this.scaleFactor.UnscaleScalar(deltaX);
  3578. }
  3579. /// <summary>
  3580. /// 获取标注样式
  3581. /// </summary>
  3582. /// <returns></returns>
  3583. public LabelStyleModel GetLabelStyleModel()
  3584. {
  3585. return this.AppWorkspaceTop.GetLabelStyleModel();
  3586. }
  3587. public PointF GetDocumentScrollPositionF()
  3588. {
  3589. return this.DocumentScrollPositionF;
  3590. //return this.AppWorkspaceTop.GetDocumentScrollPositionF();
  3591. }
  3592. public void SetDocumentScrollPositionF(PointF newScrollPos)
  3593. {
  3594. //PointF sbClientF = this.panel.SurfaceToClient(newScrollPos);
  3595. //Point sbClient = Point.Round(sbClientF);
  3596. Point sbClient = Point.Round(newScrollPos);
  3597. if (this.panel.AutoScrollPosition != new Point(-sbClient.X, -sbClient.Y))
  3598. {
  3599. this.panel.AutoScrollPosition = sbClient;
  3600. UpdateRulerOffsets();
  3601. //this.panelBottom.Invalidate();
  3602. this.topRuler.Invalidate();
  3603. this.leftRuler.Invalidate();
  3604. }
  3605. /*PointF sbClientF = this.panel.SurfaceToClient(value);
  3606. Point sbClient = Point.Round(sbClientF);
  3607. if (this.panel.AutoScrollPosition != new Point(-sbClient.X, -sbClient.Y))
  3608. {
  3609. this.panel.AutoScrollPosition = sbClient;
  3610. UpdateRulerOffsets();
  3611. this.topRuler.Invalidate();
  3612. this.leftRuler.Invalidate();
  3613. }*/
  3614. //this.AppWorkspaceTop.SetDocumentScrollPositionF(newScrollPos);
  3615. }
  3616. public string[] GetPxPerUnit()
  3617. {
  3618. return this.AppWorkspaceTop.GetPxPerUnit();//getRulerList();//.
  3619. }
  3620. public Dictionary<int, string> GetUnitsDictionary()
  3621. {
  3622. return InvariantData.unitsDictionary;
  3623. }
  3624. public Dictionary<int, string> GetUnitSymbolsDictionary()
  3625. {
  3626. return InvariantData.unitSymbolsDictionary;
  3627. }
  3628. public Dictionary<MeasurementUnit, double> getMeasureInfo()
  3629. {
  3630. if (this.rules.Count > 0)
  3631. return this.rules;
  3632. else
  3633. return this.AppWorkspaceTop.getMeasureInfo();
  3634. }
  3635. /// <summary>
  3636. /// 获取测量的样式
  3637. /// </summary>
  3638. /// <returns></returns>
  3639. public MeasureStyleModel GetMeasureStyleModel()
  3640. {
  3641. return this.AppWorkspaceTop.GetMeasureStyleModel();
  3642. }
  3643. /// <summary>
  3644. /// 获取设置-常规设置-辅助线样式信息
  3645. /// </summary>
  3646. /// <returns></returns>
  3647. public GuideStyleModel GetGuideStyleModel()
  3648. {
  3649. return this.AppWorkspaceTop.GetGuideStyleModel();
  3650. }
  3651. /// <summary>
  3652. /// 获取水印样式
  3653. /// </summary>
  3654. /// <returns></returns>
  3655. public WatermarkModel GetWatermarkModel()
  3656. {
  3657. return this.AppWorkspaceTop.GetWatermarkModel();
  3658. }
  3659. /// <summary>
  3660. /// 获取工型样式
  3661. /// </summary>
  3662. /// <returns></returns>
  3663. public WorkTypeStyleModel GetWorkTypeStyleModel()
  3664. {
  3665. return this.AppWorkspaceTop.GetWorkTypeStyleModel();
  3666. }
  3667. /// <summary>
  3668. /// 获取标尺样式
  3669. /// </summary>
  3670. /// <returns></returns>
  3671. public RulerModel GetRulerStyleModel()
  3672. {
  3673. return this.AppWorkspaceTop.GetRulerStyleModel();
  3674. }
  3675. /// <summary>
  3676. /// 获取视场的状态,合并/剪切
  3677. /// </summary>
  3678. /// <returns></returns>
  3679. public CombineMode GetCombineMode()
  3680. {
  3681. return this.AppWorkspaceTop.GetCombineMode();
  3682. }
  3683. /// <summary>
  3684. /// 设置当前鼠标的状态
  3685. /// </summary>
  3686. /// <param name="status"></param>
  3687. public void SetMouseStatus(bool status)
  3688. {
  3689. this.mouseStatus = status;
  3690. }
  3691. /// <summary>
  3692. /// 获取当前鼠标的状态
  3693. /// </summary>
  3694. public bool GetMouseStatus()
  3695. {
  3696. return this.mouseStatus;
  3697. }
  3698. public int GetSegmentationWidth()
  3699. {
  3700. return InvariantData.segmentation;
  3701. }
  3702. public int GetConnectionWidth()
  3703. {
  3704. return InvariantData.connection;
  3705. }
  3706. public decimal GetGainMultiple()
  3707. {
  3708. return this.AppWorkspaceTop.GetGainMultiple();
  3709. }
  3710. /// <summary>
  3711. /// 刷新标注列表
  3712. /// </summary>
  3713. public void RefreshLabelListDialog()
  3714. {
  3715. this.AppWorkspaceTop.RefreshLabelListDialog();
  3716. }
  3717. /// <summary>
  3718. /// 更新命名的延续数字
  3719. /// </summary>
  3720. public void UpdateContinueNum()
  3721. {
  3722. this.AppWorkspaceTop.UpdateContinueNum();
  3723. }
  3724. #endregion
  3725. #region 二值操作,对象处理
  3726. /// <summary>
  3727. /// 预处理 - 交互操作 - 单个提取
  3728. /// </summary>
  3729. /// <param name="point"></param>
  3730. public void BinaryActionExtract(PointF point)
  3731. {
  3732. PointF point1 = GetScalePoint(point);
  3733. if (point1.X >= 0 && point1.Y >= 0
  3734. && point1.X < this.CompositionSurface.Width && point1.Y < this.CompositionSurface.Height)
  3735. {
  3736. List<PhaseModel> choiseList = this.phaseModels.FindAll(a => a.choise == true);
  3737. //如果已经存在并选择了相,则在相处理,如果选择多个相,默认最后一个相
  3738. if (this.phaseModels != null && this.phaseModels.Count > 0 && choiseList.Count > 0)
  3739. {
  3740. Color color = Color.FromArgb(choiseList[0].color);
  3741. choiseList[0].mat = PreActionIntent.SingleExtract(OpenCvSharp.Extensions.BitmapConverter.ToMat(this.compositionSurface.CreateAliasedBitmap()), choiseList[0].mat, point1, new OpenCvSharp.Vec4b(color.B, color.G, color.R, 255));
  3742. this.panel.Refresh();
  3743. }
  3744. //如果不存在相或者没有选择相,则在原图上处理
  3745. else
  3746. {
  3747. PhaseModel model = new PhaseModel();
  3748. model.choise = true;
  3749. model.color = Color.Red.ToArgb();
  3750. model.position = 0;
  3751. model.name = "phase0";
  3752. model.mat = PreActionIntent.SingleExtract(OpenCvSharp.Extensions.BitmapConverter.ToMat(this.compositionSurface.CreateAliasedBitmap()), null, point1, new OpenCvSharp.Vec4b(0, 0, 255, 255));
  3753. List<PhaseModel> list = new List<PhaseModel>();
  3754. list.Add(model);
  3755. this.PhaseModels = list;
  3756. this.panel.Refresh();
  3757. }
  3758. }
  3759. }
  3760. /// <summary>
  3761. /// 预处理 - 交互操作 - 选择 - 反选
  3762. /// </summary>
  3763. public void BinaryActionAntiElection(int j)
  3764. {
  3765. OpenCvSharp.Mat[] arr = this.phaseModels[j].mat.Split();
  3766. arr[0] = ~arr[0];
  3767. arr[1] = ~arr[1];
  3768. arr[2] = ~arr[1];
  3769. OpenCvSharp.Cv2.Merge(arr, this.phaseModels[j].mat);
  3770. this.panel.Refresh();
  3771. }
  3772. /// <summary>
  3773. /// 预处理 - 交互操作 - 选择 - 应用
  3774. /// 原有的相颜色变透明,保留反色的
  3775. /// </summary>
  3776. public void BinaryActionAntiConfirm(int j)
  3777. {
  3778. this.phaseModels[j].mat = PreActionIntent.BinaryActionAntiConfirm(this.phaseModels[j].mat, this.phaseModels[j].color);
  3779. this.panel.Refresh();
  3780. }
  3781. /// <summary>
  3782. /// 预处理 - 交互操作 - 选择 - 单个选择
  3783. /// </summary>
  3784. /// <param name="point"></param>
  3785. public void BinaryActionChoise(PointF point)
  3786. {
  3787. Point point1 = GetScalePoint(point);
  3788. if (point1.X >= 0 && point1.Y >= 0
  3789. && point1.X < this.CompositionSurface.Width && point1.Y < this.CompositionSurface.Height)
  3790. {
  3791. List<PhaseModel> choiseList = this.phaseModels.FindAll(a => a.choise == true);
  3792. if (this.phaseModels != null && this.phaseModels.Count > 0 && choiseList.Count > 0)
  3793. {
  3794. foreach (PhaseModel phaseModel in choiseList)
  3795. {
  3796. phaseModel.mat = PreActionIntent.SingleChoise(phaseModel.mat, phaseModel.color, point1);
  3797. this.Refresh();
  3798. }
  3799. }
  3800. }
  3801. }
  3802. /// <summary>
  3803. /// 预处理 - 交互操作 - 选择 - 矩形选择
  3804. /// </summary>
  3805. /// <param name="rectangle"></param>
  3806. public void BinaryActionChoiseRectangle(RectangleF rectangle)
  3807. {
  3808. List<PhaseModel> choiseList = this.phaseModels.FindAll(a => a.choise == true);
  3809. if (this.phaseModels != null && this.phaseModels.Count > 0 && choiseList.Count > 0)
  3810. {
  3811. foreach (PhaseModel phaseModel in choiseList)
  3812. {
  3813. phaseModel.mat = PreActionIntent.SingleChoiseRectangle(phaseModel.mat, phaseModel.color, rectangle);
  3814. this.panel.Refresh();
  3815. }
  3816. }
  3817. }
  3818. /// <summary>
  3819. /// 预处理 - 交互操作 - 选择 - 椭圆选择
  3820. /// </summary>
  3821. /// <param name="rectangle"></param>
  3822. public void BinaryActionChoiseOval(RectangleF rectangle)
  3823. {
  3824. List<PhaseModel> choiseList = this.phaseModels.FindAll(a => a.choise == true);
  3825. if (this.phaseModels != null && this.phaseModels.Count > 0 && choiseList.Count > 0)
  3826. {
  3827. foreach (PhaseModel phaseModel in choiseList)
  3828. {
  3829. phaseModel.mat = PreActionIntent.SingleChoiseOval(phaseModel.mat, phaseModel.color, rectangle);
  3830. this.panel.Refresh();
  3831. }
  3832. }
  3833. }
  3834. /// <summary>
  3835. /// 预处理 - 交互操作 - 选择 - 多边形选择
  3836. /// </summary>
  3837. /// <param name="rectangle"></param>
  3838. public void BinaryActionChoisePolygon(List<PointF> points)
  3839. {
  3840. List<PhaseModel> choiseList = this.phaseModels.FindAll(a => a.choise == true);
  3841. if (this.phaseModels != null && this.phaseModels.Count > 0 && choiseList.Count > 0)
  3842. {
  3843. foreach (PhaseModel phaseModel in choiseList)
  3844. {
  3845. phaseModel.mat = PreActionIntent.SingleChoisePolygon(phaseModel.mat, phaseModel.color, points);
  3846. this.panel.Refresh();
  3847. }
  3848. }
  3849. }
  3850. /// <summary>
  3851. /// 预处理 - 交互操作 - 删除 - 单个删除
  3852. /// </summary>
  3853. /// <param name="point"></param>
  3854. public void BinaryActionDelete(PointF point)
  3855. {
  3856. Point point1 = GetScalePoint(point);
  3857. if (point1.X >= 0 && point1.Y >= 0
  3858. && point1.X < this.CompositionSurface.Width
  3859. && point1.Y < this.CompositionSurface.Height)
  3860. {
  3861. List<PhaseModel> choiseList = this.phaseModels.FindAll(a => a.choise == true);
  3862. if (this.phaseModels != null && this.phaseModels.Count > 0 && choiseList.Count > 0)
  3863. {
  3864. foreach (PhaseModel p in choiseList)
  3865. {
  3866. Color color = Color.FromArgb(p.color);
  3867. int c = (color.R > color.G) ? ((color.R > color.B) ? color.R : color.B) : ((color.G > color.B) ? color.G : color.B);
  3868. p.mat = PreActionIntent.SingleDelete(p.mat, point1, c);
  3869. this.panel.Refresh();
  3870. }
  3871. }
  3872. }
  3873. }
  3874. /// <summary>
  3875. /// 预处理 - 交互操作 - 删除 - 矩形删除
  3876. /// </summary>
  3877. /// <param name="rect"></param>
  3878. public void BinaryActionDelete(RectangleF rect)
  3879. {
  3880. List<PhaseModel> choiseList = this.phaseModels.FindAll(a => a.choise == true);
  3881. if (this.phaseModels != null && this.phaseModels.Count > 0 && choiseList.Count > 0)
  3882. {
  3883. foreach (PhaseModel p in choiseList)
  3884. {
  3885. p.mat = PreActionIntent.RectangleDelete(p.mat, rect);
  3886. this.panel.Refresh();
  3887. }
  3888. }
  3889. }
  3890. /// <summary>
  3891. /// 预处理 - 交互操作 - 删除 - 椭圆删除
  3892. /// </summary>
  3893. /// <param name="rect"></param>
  3894. public void BinaryActionDeleteOval(RectangleF rect)
  3895. {
  3896. List<PhaseModel> choiseList = this.phaseModels.FindAll(a => a.choise == true);
  3897. if (this.phaseModels != null && this.phaseModels.Count > 0 && choiseList.Count > 0)
  3898. {
  3899. foreach (PhaseModel p in choiseList)
  3900. {
  3901. p.mat = PreActionIntent.OvalDelete(p.mat, rect);
  3902. this.panel.Refresh();
  3903. }
  3904. }
  3905. }
  3906. /// <summary>
  3907. /// 预处理 - 交互操作 - 删除 - 多边形删除
  3908. /// </summary>
  3909. /// <param name="rect"></param>
  3910. public void BinaryActionDelete(List<PointF> points)
  3911. {
  3912. List<PhaseModel> choiseList = this.phaseModels.FindAll(a => a.choise == true);
  3913. if (this.phaseModels != null && this.phaseModels.Count > 0 && choiseList.Count > 0)
  3914. {
  3915. foreach (PhaseModel p in choiseList)
  3916. {
  3917. p.mat = PreActionIntent.PolygonDelete(p.mat, points);
  3918. this.panel.Refresh();
  3919. }
  3920. }
  3921. }
  3922. /// <summary>
  3923. /// 预处理 - 交互操作 - 直线分割
  3924. /// </summary>
  3925. public void BinaryActionSplitLine(PointF start, PointF end)
  3926. {
  3927. List<PhaseModel> choiseList = this.phaseModels.FindAll(a => a.choise == true);
  3928. if (this.phaseModels != null && this.phaseModels.Count > 0 && choiseList.Count > 0)
  3929. {
  3930. foreach (PhaseModel p in choiseList)
  3931. {
  3932. p.mat = PreActionIntent.LineSplit(p.mat, start, end, InvariantData.segmentation);
  3933. this.panel.Refresh();
  3934. }
  3935. }
  3936. }
  3937. /// <summary>
  3938. /// 预处理 - 连接 - 直线
  3939. /// </summary>
  3940. /// <param name="start"></param>
  3941. /// <param name="end"></param>
  3942. public void BinaryActionConnectionLine(PointF start, PointF end)
  3943. {
  3944. List<PhaseModel> choiseList = this.phaseModels.FindAll(a => a.choise == true);
  3945. if (this.phaseModels != null && this.phaseModels.Count > 0 && choiseList.Count > 0)
  3946. {
  3947. foreach (PhaseModel p in choiseList)
  3948. {
  3949. p.mat = PreActionIntent.LineConnection(p.mat, p.color, start, end, InvariantData.connection);
  3950. this.panel.Refresh();
  3951. }
  3952. }
  3953. }
  3954. /// <summary>
  3955. /// 预处理 - 连接 - 椭圆
  3956. /// </summary>
  3957. /// <param name="start"></param>
  3958. /// <param name="end"></param>
  3959. public void BinaryConnectionOval(RectangleF rect)
  3960. {
  3961. List<PhaseModel> choiseList = this.phaseModels.FindAll(a => a.choise == true);
  3962. if (this.phaseModels != null && this.phaseModels.Count > 0 && choiseList.Count > 0)
  3963. {
  3964. foreach (PhaseModel p in choiseList)
  3965. {
  3966. p.mat = PreActionIntent.EllipseConnection(p.mat, p.color, rect, InvariantData.connection);
  3967. this.panel.Refresh();
  3968. }
  3969. }
  3970. }
  3971. /// <summary>
  3972. /// 预处理 - 添加 - 椭圆
  3973. /// </summary>
  3974. /// <param name="start"></param>
  3975. /// <param name="end"></param>
  3976. public void BinaryActionAddOval(RectangleF rect)
  3977. {
  3978. List<PhaseModel> choiseList = this.phaseModels.FindAll(a => a.choise == true);
  3979. if (this.phaseModels != null && this.phaseModels.Count > 0 && choiseList.Count > 0)
  3980. {
  3981. foreach (PhaseModel p in choiseList)
  3982. {
  3983. p.mat = PreActionIntent.EllipseAdd(p.mat, p.color, rect);
  3984. this.panel.Refresh();
  3985. }
  3986. }
  3987. }
  3988. /// <summary>
  3989. /// 预处理 - 添加 - 矩形
  3990. /// </summary>
  3991. /// <param name="rect"></param>
  3992. public void BinaryActionAddRectangle(RectangleF rect)
  3993. {
  3994. List<PhaseModel> choiseList = this.phaseModels.FindAll(a => a.choise == true);
  3995. if (this.phaseModels != null && this.phaseModels.Count > 0 && choiseList.Count > 0)
  3996. {
  3997. foreach (PhaseModel p in choiseList)
  3998. {
  3999. p.mat = PreActionIntent.RectangleAdd(p.mat, p.color, rect);
  4000. this.panel.Refresh();
  4001. }
  4002. }
  4003. }
  4004. /// <summary>
  4005. /// 预处理 - 交互操作 - 多边形添加
  4006. /// </summary>
  4007. /// <param name="rect"></param>
  4008. public void BinaryActionAddPolygon(List<PointF> points)
  4009. {
  4010. if (points.Count > 2)
  4011. {
  4012. List<PhaseModel> choiseList = this.phaseModels.FindAll(a => a.choise == true);
  4013. if (this.phaseModels != null && this.phaseModels.Count > 0 && choiseList.Count > 0)
  4014. {
  4015. foreach (PhaseModel p in choiseList)
  4016. {
  4017. p.mat = PreActionIntent.PolygonAdd(p.mat, p.color, points);
  4018. this.panel.Refresh();
  4019. }
  4020. }
  4021. }
  4022. }
  4023. /// <summary>
  4024. /// 预处理 - 交互操作 - 椭圆分割
  4025. /// </summary>
  4026. /// <param name="rect"></param>
  4027. public void BinaryActionSplitOval(RectangleF rect)
  4028. {
  4029. List<PhaseModel> choiseList = this.phaseModels.FindAll(a => a.choise == true);
  4030. if (this.phaseModels != null && this.phaseModels.Count > 0 && choiseList.Count > 0)
  4031. {
  4032. foreach (PhaseModel p in choiseList)
  4033. {
  4034. p.mat = PreActionIntent.EllipseSplit(p.mat, rect, InvariantData.segmentation);
  4035. this.panel.Refresh();
  4036. }
  4037. }
  4038. }
  4039. #endregion
  4040. #region 物相提取
  4041. public event EventHandler<EventArgs<List<Object>>> PPhaseActionFinish;
  4042. private void OnPPhaseActionFinished(List<Object> mat)
  4043. {
  4044. if (PPhaseActionFinish != null)
  4045. {
  4046. PPhaseActionFinish(this, new EventArgs<List<Object>>(mat));
  4047. }
  4048. }
  4049. /// <summary>
  4050. /// 物相提取 - 交互操作 - 矩形选择
  4051. /// </summary>
  4052. /// <param name="point"></param>
  4053. public void PPhaseActionRectangle(RectangleF rect)
  4054. {
  4055. List<Object> objects = new List<object>();
  4056. objects.Add(ContourParameters.Rectangle);
  4057. objects.Add(rect);
  4058. OnPPhaseActionFinished(objects);
  4059. }
  4060. /// <summary>
  4061. /// 物相提取 - 交互操作 - 多边形选择
  4062. /// </summary>
  4063. /// <param name="points"></param>
  4064. public void PPhaseActionPolygon(List<PointF> points)
  4065. {
  4066. List<Object> objects = new List<object>();
  4067. objects.Add(ContourParameters.Polygon);
  4068. objects.Add(points);
  4069. OnPPhaseActionFinished(objects);
  4070. }
  4071. /// <summary>
  4072. /// 物相提取 - 交互操作 - 椭圆选择
  4073. /// </summary>
  4074. /// <param name="rect"></param>
  4075. public void PPhaseActionOval(RectangleF rect)
  4076. {
  4077. List<Object> objects = new List<object>();
  4078. objects.Add(ContourParameters.Oval);
  4079. objects.Add(rect);
  4080. OnPPhaseActionFinished(objects);
  4081. }
  4082. #endregion
  4083. #region 视场相关
  4084. /// <summary>
  4085. /// 获取界面中的视场区域
  4086. /// </summary>
  4087. /// <returns></returns>
  4088. public Region GetRegion()
  4089. {
  4090. Region a = null;
  4091. if (this.GraphicsList != null && this.GraphicsList.Count > 0)
  4092. {
  4093. for (int i = this.GraphicsList.Count - 1; i >= 0; i--)
  4094. {
  4095. if (this.GraphicsList[i].objectType == DrawClass.View)
  4096. {
  4097. ViewBase view = (ViewBase)this.GraphicsList[i];
  4098. GraphicsPath path = new GraphicsPath();
  4099. switch (view.drawToolType)
  4100. {
  4101. case DrawToolType.ViewOval:
  4102. path.AddClosedCurve(view.Points.ToArray());
  4103. break;
  4104. case DrawToolType.ViewCircle:
  4105. path.AddEllipse(view.Rectangle);
  4106. break;
  4107. case DrawToolType.ViewRectangle:
  4108. case DrawToolType.ViewSquare:
  4109. case DrawToolType.ViewTriangle:
  4110. case DrawToolType.ViewPolygon:
  4111. case DrawToolType.ViewRectangleEx:
  4112. case DrawToolType.ViewTriangleEx:
  4113. if (view.GetPoints().Count > 2)
  4114. {
  4115. path.AddPolygon(view.GetPoints().ToArray());
  4116. }
  4117. break;
  4118. }
  4119. if (a == null)
  4120. {
  4121. a = new Region(path);
  4122. }
  4123. else
  4124. {
  4125. if (view.combineMode == CombineMode.Union)
  4126. a.Union(path);
  4127. else
  4128. a.Exclude(path);
  4129. }
  4130. }
  4131. }
  4132. }
  4133. return a;
  4134. }
  4135. /// <summary>
  4136. /// 剪切视场/透明剪切(通过颜色区分)
  4137. /// </summary>
  4138. /// <param name="transparent">是否透明</param>
  4139. /// <returns></returns>
  4140. public Bitmap GetCutSizeWithColorWhiteOrTransparentColor(bool transparent)
  4141. {
  4142. Region region = this.GetRegion();
  4143. Bitmap bitmap = this.CompositionSurface.CreateAliasedBitmap();
  4144. if (region != null)
  4145. {
  4146. //创建新图位图
  4147. Bitmap newBitmap = new Bitmap(bitmap.Width, bitmap.Height);
  4148. //将新位图全部设置一个背景色
  4149. Graphics newGraphic = Graphics.FromImage(newBitmap);
  4150. newGraphic.Clear(Color.FromArgb(0, 0, 0, 0));
  4151. //设置新位图绘制区域
  4152. newGraphic.Clip = region;
  4153. //绘制白色背景色
  4154. newGraphic.Clear(Color.FromArgb(255, 255, 255, 255));
  4155. return newBitmap;
  4156. }
  4157. return bitmap;
  4158. }
  4159. /// <summary>
  4160. /// 剪切视场/透明剪切
  4161. /// </summary>
  4162. /// <param name="transparent">是否透明</param>
  4163. /// <returns></returns>
  4164. public Bitmap GetCutSizeWithColorWhiteOrTransparent(bool transparent)
  4165. {
  4166. Region region = this.GetRegion();
  4167. if (region != null)
  4168. {
  4169. //获取当前激活的图片
  4170. Bitmap bitmap = this.CompositionSurface.CreateAliasedBitmap();
  4171. //创建同样大小的新位图
  4172. Bitmap newBitmap = new Bitmap((int)(bitmap.Width), (int)(bitmap.Height));
  4173. //创建作图区域
  4174. Graphics newGraphic = Graphics.FromImage(newBitmap);
  4175. //如果不是透明,则用白色填充
  4176. if (!transparent) newGraphic.Clear(Color.White);
  4177. //设置新位图绘制区域
  4178. newGraphic.Clip = region;
  4179. //截取原图相应区域写入作图区
  4180. newGraphic.DrawImage(bitmap, 0, 0, new RectangleF(0, 0, bitmap.Width, bitmap.Height), GraphicsUnit.Pixel);
  4181. //创建region的RectangleF大小的位图
  4182. Graphics graphic = Graphics.FromImage(bitmap);
  4183. RectangleF rectangleF = region.GetBounds(graphic);
  4184. Bitmap lastBitmap = new Bitmap((int)(rectangleF.Width), (int)(rectangleF.Height));
  4185. Graphics lastGraphic = Graphics.FromImage(lastBitmap);
  4186. lastGraphic.DrawImage(newBitmap, 0, 0, region.GetBounds(graphic), GraphicsUnit.Pixel);
  4187. return lastBitmap;
  4188. }
  4189. return null;
  4190. }
  4191. /// <summary>
  4192. /// 获取原图大小的Bitmap
  4193. /// 除了视场外的其余均为透明(但保留各通道像素值),
  4194. /// 主要是用于二值提取、物相提取等和视场关联的地方
  4195. /// </summary>
  4196. /// <returns></returns>
  4197. public Bitmap GetFullSizeWithRegion(Bitmap bitmap = null)
  4198. {
  4199. Region region = this.GetRegion();
  4200. if (region != null)
  4201. {
  4202. if (bitmap == null) bitmap = this.CompositionSurface.CreateAliasedBitmap();
  4203. //创建同样的像素值的新位图,不过是透明的
  4204. Bitmap newBitmap = BaseTools.BitmapToTransparent(bitmap);
  4205. //创建作图区域
  4206. Graphics newGraphic = Graphics.FromImage(newBitmap);
  4207. //设置新位图绘制区域
  4208. newGraphic.Clip = region;
  4209. //截取原图相应区域写入作图区
  4210. newGraphic.DrawImage(bitmap, 0, 0, new RectangleF(0, 0, bitmap.Width, bitmap.Height), GraphicsUnit.Pixel);
  4211. return newBitmap;
  4212. }
  4213. return null;
  4214. }
  4215. #endregion
  4216. #region 初始化控件
  4217. private void InitializeComponent()
  4218. {
  4219. this.components = new Container();
  4220. this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
  4221. this.topRuler = new Ruler();
  4222. this.leftRuler = new Ruler();
  4223. this.panel = new PanelEx();
  4224. this.toolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
  4225. this.toolStripMenuItem2 = new System.Windows.Forms.ToolStripMenuItem();
  4226. this.toolStripMenuItem3 = new System.Windows.Forms.ToolStripMenuItem();
  4227. this.toolStripMenuItem4 = new System.Windows.Forms.ToolStripMenuItem();
  4228. //this.panelBottom = new PanelBottom();
  4229. //this.panelBottom.SuspendLayout();
  4230. this.panel.SuspendLayout();
  4231. this.contextMenuStrip1.SuspendLayout();
  4232. this.SuspendLayout();
  4233. //
  4234. // topRuler
  4235. //
  4236. this.topRuler.BackColor = Color.White;
  4237. this.topRuler.Dock = DockStyle.Top;
  4238. this.topRuler.Location = new Point(0, 0);
  4239. this.topRuler.Name = "topRuler";
  4240. this.topRuler.Offset = -16;
  4241. this.topRuler.Size = UI.ScaleSize(new Size(384, 16));
  4242. this.topRuler.TabIndex = 3;
  4243. //
  4244. // leftRuler
  4245. //
  4246. this.leftRuler.BackColor = Color.White;
  4247. this.leftRuler.Dock = DockStyle.Left;
  4248. this.leftRuler.Location = new Point(0, 16);
  4249. this.leftRuler.Name = "leftRuler";
  4250. this.leftRuler.Orientation = Orientation.Vertical;
  4251. this.leftRuler.Size = UI.ScaleSize(new Size(16, 304));
  4252. this.leftRuler.TabIndex = 4;
  4253. //
  4254. // panel
  4255. //
  4256. this.panel.ContextMenuStrip = this.contextMenuStrip1;
  4257. this.panel.AutoScroll = true;
  4258. this.panel.Dock = DockStyle.Fill;
  4259. this.panel.Location = new Point(16, 16);
  4260. this.panel.Name = "panel";
  4261. this.panel.ScrollPosition = new Point(0, 0);
  4262. this.panel.Size = new Size(368, 304);
  4263. this.panel.TabIndex = 5;
  4264. this.panel.AllowDrop = false;
  4265. this.panel.Scroll += new ScrollEventHandler(this.Panel_Scroll);
  4266. this.panel.KeyDown += new KeyEventHandler(Panel_KeyDown);
  4267. this.panel.KeyUp += new KeyEventHandler(Panel_KeyUp);
  4268. this.panel.KeyPress += new KeyPressEventHandler(Panel_KeyPress);
  4269. this.panel.GotFocus += new EventHandler(Panel_GotFocus);
  4270. this.panel.LostFocus += new EventHandler(Panel_LostFocus);
  4271. //
  4272. // contextMenuStrip1
  4273. //
  4274. this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
  4275. this.toolStripMenuItem1,
  4276. this.toolStripMenuItem2,
  4277. this.toolStripMenuItem3,
  4278. this.toolStripMenuItem4});
  4279. this.contextMenuStrip1.Name = "contextMenuStrip1";
  4280. this.contextMenuStrip1.Size = new System.Drawing.Size(69, 48);
  4281. //this.contextMenuStrip1.Opening += new System.ComponentModel.CancelEventHandler(this.ContextMenuStrip1_Opening);
  4282. this.contextMenuStrip1.Visible = false;
  4283. //
  4284. //
  4285. // toolStripMenuItem1
  4286. //
  4287. this.toolStripMenuItem1.Name = "toolStripMenuItem1";
  4288. this.toolStripMenuItem1.Text = PdnResources.GetString("Menu.Setting.MeasureSetting.Text");
  4289. this.toolStripMenuItem1.Size = new System.Drawing.Size(68, 22);
  4290. this.toolStripMenuItem1.Click += new System.EventHandler(this.ToolStripMenuItem1_Click);
  4291. this.toolStripMenuItem1.Visible = false;
  4292. //
  4293. // toolStripMenuItem2
  4294. //
  4295. this.toolStripMenuItem2.Name = "toolStripMenuItem2";
  4296. this.toolStripMenuItem2.Text = PdnResources.GetString("Menu.Setting.LabelSetting.Text");
  4297. this.toolStripMenuItem2.Size = new System.Drawing.Size(68, 22);
  4298. this.toolStripMenuItem2.Click += new System.EventHandler(this.ToolStripMenuItem2_Click);
  4299. this.toolStripMenuItem2.Visible = false;
  4300. //
  4301. // toolStripMenuItem3
  4302. //
  4303. this.toolStripMenuItem3.Name = "toolStripMenuItem3";
  4304. this.toolStripMenuItem3.Text = PdnResources.GetString("Menu.3celaing.Text");
  4305. this.toolStripMenuItem3.Size = new System.Drawing.Size(68, 22);
  4306. this.toolStripMenuItem3.Click += new System.EventHandler(this.ToolStripMenuItem3_Click);
  4307. this.toolStripMenuItem3.Visible = false;
  4308. //
  4309. // toolStripMenuItem4
  4310. //
  4311. this.toolStripMenuItem4.Name = "toolStripMenuItem4";
  4312. this.toolStripMenuItem4.Text = PdnResources.GetString("Menu.biaozhushuxing.Text");
  4313. this.toolStripMenuItem4.Size = new System.Drawing.Size(68, 22);
  4314. this.toolStripMenuItem4.Click += new System.EventHandler(this.ToolStripMenuItem4_Click);
  4315. this.toolStripMenuItem4.Visible = false;
  4316. // 底部的panelBottom
  4317. //
  4318. /*this.panelBottom.Size = new Size(384, 30);
  4319. this.panelBottom.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom;
  4320. this.panelBottom.Dock = DockStyle.Bottom;
  4321. this.panelBottom.trackBar.ValueChanged += new EventHandler(this.PanelBottom_trackBar_ValueChanged);
  4322. this.panelBottom.BackColor = Color.FromArgb(240, 240, 240);*/
  4323. //
  4324. // DocumentView
  4325. //
  4326. this.Controls.Add(this.panel);
  4327. this.Controls.Add(this.leftRuler);
  4328. this.Controls.Add(this.topRuler);
  4329. //this.Controls.Add(this.panelBottom);
  4330. this.Name = "DocumentView";
  4331. this.Size = new System.Drawing.Size(384, 320);
  4332. this.panel.ResumeLayout(false);
  4333. //this.panelBottom.ResumeLayout(false);
  4334. this.contextMenuStrip1.ResumeLayout(false);
  4335. this.ResumeLayout(false);
  4336. }
  4337. public virtual void ShowContextMenuStrip1()
  4338. {
  4339. this.contextMenuStrip1.Visible = true;
  4340. if (toolNumber == 0)
  4341. {
  4342. contextMenuStrip1.Visible = true;
  4343. this.toolStripMenuItem1.Visible = true;
  4344. this.toolStripMenuItem2.Visible = true;
  4345. this.toolStripMenuItem3.Visible = false;
  4346. this.toolStripMenuItem4.Visible = false;
  4347. }
  4348. else if (toolNumber == 1)
  4349. {
  4350. contextMenuStrip1.Visible = true;
  4351. this.toolStripMenuItem1.Visible = false;
  4352. this.toolStripMenuItem2.Visible = false;
  4353. this.toolStripMenuItem3.Visible = true;
  4354. this.toolStripMenuItem4.Visible = false;
  4355. }
  4356. else if (toolNumber == 2)
  4357. {
  4358. if (IsScratchesOrStains())
  4359. {
  4360. contextMenuStrip1.Visible = false;
  4361. this.toolStripMenuItem1.Visible = false;
  4362. this.toolStripMenuItem2.Visible = false;
  4363. this.toolStripMenuItem3.Visible = false;
  4364. this.toolStripMenuItem4.Visible = false;
  4365. }
  4366. else
  4367. {
  4368. contextMenuStrip1.Visible = true;
  4369. this.toolStripMenuItem1.Visible = false;
  4370. this.toolStripMenuItem2.Visible = false;
  4371. this.toolStripMenuItem3.Visible = false;
  4372. this.toolStripMenuItem4.Visible = true;
  4373. }
  4374. }
  4375. else
  4376. {
  4377. contextMenuStrip1.Visible = false;
  4378. this.toolStripMenuItem1.Visible = false;
  4379. this.toolStripMenuItem2.Visible = false;
  4380. this.toolStripMenuItem3.Visible = false;
  4381. this.toolStripMenuItem4.Visible = false;
  4382. }
  4383. }
  4384. //是否是污迹或者划痕处理
  4385. bool IsScratchesOrStains()
  4386. {
  4387. bool IsScratchesOrStains = false;
  4388. for (int i = 0; i < this.GraphicsList.Count; i++)
  4389. {
  4390. if (this.GraphicsList[i].Selected)
  4391. {
  4392. //判断如果是划痕处理
  4393. if (this.GraphicsList[i].drawToolType == DrawToolType.DrawScratchTreatmentLine || this.GraphicsList[i].drawToolType == DrawToolType.DrawSmudgeRectangle || this.GraphicsList[i].drawToolType == DrawToolType.DrawSmudgePolygon || this.GraphicsList[i].drawToolType == DrawToolType.DrawSmudgeCircle || this.GraphicsList[i].drawToolType == DrawToolType.DrawSmudgeEllipse)
  4394. {
  4395. IsScratchesOrStains = true;
  4396. }
  4397. }
  4398. }
  4399. return IsScratchesOrStains;
  4400. }
  4401. #endregion
  4402. #region 设定图片位置
  4403. // add by maxb 20200716
  4404. /// <summary>
  4405. /// 图片位置坐标
  4406. /// </summary>
  4407. private bool m_center = true;
  4408. private int m_pTop = 0;
  4409. private int m_pLeft = 0;
  4410. private int m_initTop = 0;
  4411. private int m_initLeft = 0;
  4412. public void SetDocumentPoint(int x, int y)
  4413. {
  4414. m_center = false;
  4415. m_initLeft = x;
  4416. m_initTop = y;
  4417. UpdateImgLocation();
  4418. this.Refresh();
  4419. }
  4420. public void SetCenter(bool center)
  4421. {
  4422. m_center = center;
  4423. }
  4424. private void UpdateImgLocation()
  4425. {
  4426. if (!m_center)
  4427. {
  4428. //Console.WriteLine("m_pLeft: " + m_initLeft + " Ratio:" + this.scaleFactor.Ratio);
  4429. m_pLeft = 16 + (int)(m_initLeft * this.scaleFactor.Ratio);
  4430. m_pTop = 16 + (int)(m_initTop * this.scaleFactor.Ratio);
  4431. //Console.WriteLine("Location: " + m_pLeft + " " + m_pTop);
  4432. }
  4433. }
  4434. #endregion
  4435. #region 工艺图比照
  4436. private RectangleF m_artworkImageRectangle;
  4437. private Bitmap m_selectedBitmap;
  4438. /// <summary>
  4439. /// 工艺比照定位点图像绘制
  4440. /// </summary>
  4441. /// <param name="bitmap"></param>
  4442. /// <param name="rectangle"></param>
  4443. public void SetSelectedBitmap(Bitmap bitmap, RectangleF rectangle)
  4444. {
  4445. m_selectedBitmap = bitmap;
  4446. m_artworkImageRectangle = Rectangle.Ceiling(rectangle);
  4447. this.Refresh();
  4448. }
  4449. /// <summary>
  4450. /// 工艺比照定位点
  4451. /// </summary>
  4452. /// <param name="width"></param>
  4453. /// <param name="height"></param>
  4454. public void CreateArtworkRectangle(int id, Pen pen, Rectangle rect)
  4455. {
  4456. tools[_activeTool].InvokeMember("CreateRectangle",
  4457. BindingFlags.Public |
  4458. BindingFlags.Static |
  4459. BindingFlags.InvokeMethod,
  4460. null,
  4461. null,
  4462. new object[4] { this, id, pen, rect });
  4463. }
  4464. #endregion
  4465. #region 图像拼接
  4466. /// <summary>
  4467. /// 生成自定义拼图区域
  4468. /// </summary>
  4469. /// <param name="width"></param>
  4470. /// <param name="height"></param>
  4471. public void CreateStitchingRectangle(int width, int height)
  4472. {
  4473. int x = m_pLeft;
  4474. int y = m_pTop;
  4475. tools[_activeTool].InvokeMember("CreateRectangle",
  4476. BindingFlags.Public |
  4477. BindingFlags.Static |
  4478. BindingFlags.InvokeMethod,
  4479. null,
  4480. null,
  4481. new object[5] { this, x, y, width, height });//
  4482. }
  4483. /// <summary>
  4484. /// 获取对应标尺上的坐标点
  4485. /// </summary>
  4486. /// <param name="point"></param>
  4487. /// <returns></returns>
  4488. public PointF GetRulerPointInPanel(PointF point)
  4489. {
  4490. return new PointF(
  4491. (float)((point.X - 16) / this.scaleFactor.Ratio),
  4492. (float)((point.Y - 16) / this.scaleFactor.Ratio)
  4493. );
  4494. }
  4495. public PointF ScalePointToRulerPoint(PointF point)
  4496. {
  4497. Point originPoint = GetCalcOriginPoint();
  4498. return new PointF(
  4499. (float)((point.X * this.scaleFactor.Ratio + originPoint.X - 16) / this.scaleFactor.Ratio),
  4500. (float)((point.Y * this.scaleFactor.Ratio + originPoint.Y - 16) / this.scaleFactor.Ratio)
  4501. );
  4502. }
  4503. /// <summary>
  4504. /// 是否开启鼠标左键双击移动视场
  4505. /// </summary>
  4506. public bool ViewMoveOnMouseLeftDoubleClickEnable
  4507. {
  4508. get; set;
  4509. }
  4510. #endregion
  4511. #region 位置列表
  4512. /// <summary>
  4513. /// 生成自定义拼图区域
  4514. /// </summary>
  4515. /// <param name="width"></param>
  4516. /// <param name="height"></param>
  4517. public void CreateLocationCross(int x, int y, int width, int height, int z)
  4518. {
  4519. //m_pLeft = x;
  4520. //m_pTop = y;
  4521. x = (int)(m_pLeft + x * this.scaleFactor.Ratio);
  4522. y = (int)(m_pTop + y * this.scaleFactor.Ratio);
  4523. ToolLocationCross.CreateLocationCross(this, x, y, width, height, z);//
  4524. }
  4525. #endregion
  4526. #region 右键属性
  4527. /// <summary>
  4528. /// 判断右键的工具
  4529. /// </summary>
  4530. public int ToolNumber
  4531. {
  4532. set
  4533. {
  4534. this.toolNumber = value;
  4535. }
  4536. }
  4537. /// <summary>
  4538. /// 设置右键菜单的选项
  4539. /// </summary>
  4540. /// <param name="sender"></param>
  4541. /// <param name="e"></param>
  4542. private void ContextMenuStrip1_Opening(object sender, CancelEventArgs e)
  4543. {
  4544. if (toolNumber == 0)
  4545. {
  4546. contextMenuStrip1.Visible = true;
  4547. this.toolStripMenuItem1.Visible = true;
  4548. this.toolStripMenuItem2.Visible = true;
  4549. this.toolStripMenuItem3.Visible = false;
  4550. this.toolStripMenuItem4.Visible = false;
  4551. }
  4552. else if (toolNumber == 1)
  4553. {
  4554. contextMenuStrip1.Visible = true;
  4555. this.toolStripMenuItem1.Visible = false;
  4556. this.toolStripMenuItem2.Visible = false;
  4557. this.toolStripMenuItem3.Visible = true;
  4558. this.toolStripMenuItem4.Visible = false;
  4559. }
  4560. else if (toolNumber == 2)
  4561. {
  4562. contextMenuStrip1.Visible = true;
  4563. this.toolStripMenuItem1.Visible = false;
  4564. this.toolStripMenuItem2.Visible = false;
  4565. this.toolStripMenuItem3.Visible = false;
  4566. this.toolStripMenuItem4.Visible = true;
  4567. }
  4568. else
  4569. {
  4570. contextMenuStrip1.Visible = false;
  4571. this.toolStripMenuItem1.Visible = false;
  4572. this.toolStripMenuItem2.Visible = false;
  4573. this.toolStripMenuItem3.Visible = false;
  4574. this.toolStripMenuItem4.Visible = false;
  4575. }
  4576. }
  4577. /// <summary>
  4578. /// 右键菜单(测量设置)
  4579. /// </summary>
  4580. /// <param name="sender"></param>
  4581. /// <param name="e"></param>
  4582. public virtual void ToolStripMenuItem1_Click(object sender, EventArgs e)
  4583. {
  4584. }
  4585. /// <summary>
  4586. /// 右键菜单(标注设置)
  4587. /// </summary>
  4588. /// <param name="sender"></param>
  4589. /// <param name="e"></param>
  4590. public virtual void ToolStripMenuItem2_Click(object sender, EventArgs e)
  4591. {
  4592. }
  4593. /// <summary>
  4594. /// 右键菜单(测量属性)
  4595. /// </summary>
  4596. /// <param name="sender"></param>
  4597. /// <param name="e"></param>
  4598. public virtual void ToolStripMenuItem3_Click(object sender, EventArgs e)
  4599. {
  4600. }
  4601. /// <summary>
  4602. /// 右键菜单(标注属性)
  4603. /// </summary>
  4604. /// <param name="sender"></param>
  4605. /// <param name="e"></param>
  4606. public virtual void ToolStripMenuItem4_Click(object sender, EventArgs e)
  4607. {
  4608. }
  4609. #endregion
  4610. #region 对应图片保存的xml参数
  4611. public bool existenceXML = false;//当前图片是否存在对应的xml
  4612. public mic_rulers xmlSaveModel;//存储标尺信息(xml或者预览拍摄选中标尺)
  4613. /// <summary>
  4614. /// 换算完的标尺信息,包含所有系统内得单位
  4615. /// </summary>
  4616. public Dictionary<MeasurementUnit, double> rules = new Dictionary<MeasurementUnit, double>();
  4617. /// <summary>
  4618. /// AxioVision标尺单位对应
  4619. /// </summary>
  4620. public Dictionary<int, MeasurementUnit> rule = new Dictionary<int, MeasurementUnit>() { { 76, MeasurementUnit.Micron},{75, MeasurementUnit.Millimeter},{ 74, MeasurementUnit.Centimeter}, { 77, MeasurementUnit .Nano} ,{ 81, MeasurementUnit.Inch},{ 84, MeasurementUnit.Mil} };
  4621. ///// <summary>
  4622. ///// 系统内选定的单位
  4623. ///// </summary>
  4624. //public MeasurementUnit measurementUnit;
  4625. public mic_rulers RuleAttribute(PicConfigModel PicConfigModel)
  4626. {
  4627. this.xmlSaveModel = new mic_rulers();
  4628. //this.xmlSaveModel.rule = new XmlSaveModel.Rule();
  4629. this.xmlSaveModel.ruler_name = PicConfigModel.rule.ruler_name;
  4630. this.xmlSaveModel.gain_multiple = PicConfigModel.rule.gain_multiple;
  4631. this.xmlSaveModel.pixel_length = PicConfigModel.rule.pixel_length;
  4632. this.xmlSaveModel.physical_length = PicConfigModel.rule.physical_length;
  4633. this.xmlSaveModel.ruler_units = PicConfigModel.rule.ruler_units;
  4634. InitRulerInfo();
  4635. this.existenceXML = true;
  4636. return this.xmlSaveModel;
  4637. }
  4638. /// <summary>
  4639. /// 获取AxioVision标尺(提取方式不确定,后续实时变更)
  4640. /// RuleAttr[0]单位标识,RuleAttr[1]标尺长度
  4641. /// </summary>
  4642. /// <param name="xmlPath">标尺路径</param>
  4643. /// <returns></returns>
  4644. public mic_rulers RuleAttribute(string name,double rulerData, MeasurementUnit measurement,decimal gain_multiple)
  4645. {
  4646. this.xmlSaveModel = new mic_rulers();
  4647. this.xmlSaveModel.ruler_name = name;
  4648. this.xmlSaveModel.gain_multiple = gain_multiple;
  4649. this.xmlSaveModel.pixel_length = 100;
  4650. this.xmlSaveModel.physical_length = Convert.ToDecimal(100 * rulerData);
  4651. this.xmlSaveModel.ruler_units = (int)measurement;
  4652. InitRulerInfo();
  4653. this.existenceXML = true;
  4654. return this.xmlSaveModel;
  4655. }
  4656. //显微镜硬件参数MODEL
  4657. public Hardware hardware;
  4658. public void HardwareAttribute()
  4659. {
  4660. this.hardware = new Hardware();
  4661. }
  4662. public decimal GetMic_Rulers()
  4663. {
  4664. if (this.xmlSaveModel != null)
  4665. {
  4666. return this.xmlSaveModel.gain_multiple;
  4667. }
  4668. else
  4669. {
  4670. return appWorkspace.GetGainMultiple();
  4671. }
  4672. }
  4673. /// <summary>
  4674. /// 初始化标尺信息,图片对应的xml中读取
  4675. /// 然后根据标尺的单位,换算出所有单位的数据
  4676. /// </summary>
  4677. public void InitRulerInfo()
  4678. {
  4679. List<mic_rulers> mic_rulersAll = Mic_rulersAll();
  4680. if (this.xmlSaveModel == null && mic_rulersAll.Count > 0)
  4681. this.xmlSaveModel = mic_rulersAll[0];
  4682. this.rules.Clear();
  4683. if (this.xmlSaveModel != null && this.xmlSaveModel.pixel_length != 0)
  4684. {
  4685. //计算单位长度 比如0.05英寸/像素; 100纳米/像素;
  4686. double unitLength = (double)(this.xmlSaveModel.physical_length / (decimal)this.xmlSaveModel.pixel_length);
  4687. switch (this.xmlSaveModel.ruler_units)
  4688. {
  4689. case (int)MeasurementUnit.Inch://英寸
  4690. this.rules.Add(MeasurementUnit.Inch, unitLength); //英寸
  4691. this.rules.Add(MeasurementUnit.Mil, 1000 * unitLength); //米尔
  4692. this.rules.Add(MeasurementUnit.Centimeter, 2.54 * unitLength); //厘米
  4693. this.rules.Add(MeasurementUnit.Millimeter, 25.4 * unitLength); //毫米
  4694. this.rules.Add(MeasurementUnit.Micron, 25400 * unitLength); //微米
  4695. this.rules.Add(MeasurementUnit.Nano, 25400000 * unitLength); //纳米
  4696. break;
  4697. case (int)MeasurementUnit.Mil://米尔
  4698. this.rules.Add(MeasurementUnit.Inch, 0.001 * unitLength); //英寸
  4699. this.rules.Add(MeasurementUnit.Mil, unitLength); //米尔
  4700. this.rules.Add(MeasurementUnit.Centimeter, 0.00254 * unitLength); //厘米
  4701. this.rules.Add(MeasurementUnit.Millimeter, 0.0254 * unitLength); //毫米
  4702. this.rules.Add(MeasurementUnit.Micron, 25.4 * unitLength); //微米
  4703. this.rules.Add(MeasurementUnit.Nano, 25400 * unitLength); //纳米
  4704. break;
  4705. case (int)MeasurementUnit.Centimeter://厘米
  4706. this.rules.Add(MeasurementUnit.Inch, 0.3937008 * unitLength); //英寸
  4707. this.rules.Add(MeasurementUnit.Mil, 393.7008 * unitLength); //米尔
  4708. this.rules.Add(MeasurementUnit.Centimeter, unitLength); //厘米
  4709. this.rules.Add(MeasurementUnit.Millimeter, 10 * unitLength); //毫米
  4710. this.rules.Add(MeasurementUnit.Micron, 10000 * unitLength); //微米
  4711. this.rules.Add(MeasurementUnit.Nano, 10000000 * unitLength); //纳米
  4712. break;
  4713. case (int)MeasurementUnit.Millimeter://毫米
  4714. this.rules.Add(MeasurementUnit.Inch, 0.0393701 * unitLength); //英寸
  4715. this.rules.Add(MeasurementUnit.Mil, 39.3701 * unitLength); //米尔
  4716. this.rules.Add(MeasurementUnit.Centimeter, 0.1 * unitLength); //厘米
  4717. this.rules.Add(MeasurementUnit.Millimeter, unitLength); //毫米
  4718. this.rules.Add(MeasurementUnit.Micron, 1000 * unitLength); //微米
  4719. this.rules.Add(MeasurementUnit.Nano, 1000000 * unitLength); //纳米
  4720. break;
  4721. case (int)MeasurementUnit.Micron://微米
  4722. this.rules.Add(MeasurementUnit.Inch, 0.00003937007874 * unitLength); //英寸
  4723. this.rules.Add(MeasurementUnit.Mil, 0.03937007874 * unitLength); //米尔
  4724. this.rules.Add(MeasurementUnit.Centimeter, 0.0001 * unitLength); //厘米
  4725. this.rules.Add(MeasurementUnit.Millimeter, 0.001 * unitLength); //毫米
  4726. this.rules.Add(MeasurementUnit.Micron, unitLength); //微米
  4727. this.rules.Add(MeasurementUnit.Nano, 1000 * unitLength); //纳米
  4728. break;
  4729. case (int)MeasurementUnit.Nano://纳米
  4730. this.rules.Add(MeasurementUnit.Inch, 3.9370e-8 * unitLength); //英寸
  4731. this.rules.Add(MeasurementUnit.Mil, 3.9370e-5 * unitLength); //米尔
  4732. this.rules.Add(MeasurementUnit.Centimeter, 1e-7 * unitLength); //厘米
  4733. this.rules.Add(MeasurementUnit.Millimeter, 1e-6 * unitLength); //毫米
  4734. this.rules.Add(MeasurementUnit.Micron, 0.001 * unitLength); //微米
  4735. this.rules.Add(MeasurementUnit.Nano, unitLength); //纳米
  4736. break;
  4737. default:
  4738. this.xmlSaveModel = null;
  4739. break;
  4740. }
  4741. }
  4742. if (this.xmlSaveModel == null || this.xmlSaveModel.physical_length == 0)
  4743. {
  4744. //如果没有标尺的时候,默认1微米/像素
  4745. this.rules.Add(MeasurementUnit.Inch, 0.0000394); //英寸
  4746. this.rules.Add(MeasurementUnit.Mil, 0.0394); //米尔
  4747. this.rules.Add(MeasurementUnit.Centimeter, 0.0001); //厘米
  4748. this.rules.Add(MeasurementUnit.Millimeter, 0.001); //毫米
  4749. this.rules.Add(MeasurementUnit.Micron, 1); //微米
  4750. this.rules.Add(MeasurementUnit.Nano, 1000); //纳米
  4751. this.xmlSaveModel = new mic_rulers();
  4752. this.xmlSaveModel.ruler_name = PdnResources.GetString("Menu.Unselectedruler.Text");
  4753. this.xmlSaveModel.gain_multiple = 1;
  4754. this.xmlSaveModel.physical_length = 100;
  4755. this.xmlSaveModel.pixel_length = 100;
  4756. this.xmlSaveModel.ruler_units = (int)((MeasurementUnit)System.Enum.Parse(typeof(MeasurementUnit), this.getRulerList()[0]));
  4757. }
  4758. //设置文档标尺
  4759. Document.defaultDpi = 1 / rules[MeasurementUnit.Inch];
  4760. }
  4761. /// <summary>
  4762. /// 提供获取系统当前选中单位及每单位像素值公共方法
  4763. /// </summary>
  4764. /// <returns></returns>
  4765. public string[] getRulerList()
  4766. {
  4767. return startUpRules(this.rules.Count > 0 ? this.rules : null);
  4768. }
  4769. /// <summary>
  4770. /// 获取单位标尺
  4771. /// </summary>
  4772. /// <param name="measurementUnit">度量单位的枚举</param>
  4773. /// <returns></returns>
  4774. public double GetRuler(MeasurementUnit measurementUnit)
  4775. {
  4776. double unitLength = 0;
  4777. //判断当前图片是否有对应的标尺
  4778. if (this.rules.Count > 0)
  4779. {
  4780. this.rules.TryGetValue(measurementUnit, out unitLength);
  4781. }
  4782. else
  4783. {
  4784. this.appWorkspace.getMeasureInfo().TryGetValue(MeasurementUnit.Micron, out unitLength);
  4785. }
  4786. return unitLength;
  4787. }
  4788. /// <summary>
  4789. /// 获取放大倍数
  4790. /// </summary>
  4791. /// <returns></returns>
  4792. public double GetMultiple()
  4793. {
  4794. double multiple = 1;
  4795. if (this.xmlSaveModel != null)
  4796. {
  4797. multiple = (double)this.xmlSaveModel.gain_multiple;
  4798. }
  4799. return multiple;
  4800. }
  4801. /// <summary>
  4802. /// 获取单位标尺乘以放大倍数
  4803. /// </summary>
  4804. /// <param name="measurementUnit">度量单位的枚举</param>
  4805. /// <returns></returns>
  4806. public double GetRulerMultiple(MeasurementUnit measurementUnit)
  4807. {
  4808. double unitLength = 0;
  4809. double multiple = 0;
  4810. //判断当前图片是否有对应的标尺
  4811. if (this.rules.Count > 0)
  4812. {
  4813. this.rules.TryGetValue(measurementUnit, out unitLength);
  4814. }
  4815. else
  4816. {
  4817. this.appWorkspace.getMeasureInfo().TryGetValue(MeasurementUnit.Micron, out unitLength);
  4818. }
  4819. if (this.xmlSaveModel != null)
  4820. {
  4821. multiple = (double)this.xmlSaveModel.gain_multiple;
  4822. }
  4823. return unitLength * multiple;
  4824. }
  4825. public void SetContinuousDrawingLabel(bool v)
  4826. {
  4827. ContinuousDrawingLabel = v;
  4828. appWorkspace.SetContinuousDrawingLable(v);
  4829. }
  4830. public void SetContinuousDrawingMeasure(bool v)
  4831. {
  4832. ContinuousDrawingMeasure = v;
  4833. appWorkspace.SetContinuousDrawingMeasure(v);
  4834. }
  4835. #endregion
  4836. public Dictionary<DrawToolType, Type> GetTools()
  4837. {
  4838. return tools;
  4839. }
  4840. public MeasurementUnit GetMeasurementUnit()
  4841. {
  4842. return this.Units;
  4843. }
  4844. public Inclusion GetInclusion()
  4845. {
  4846. return this.inclusion;
  4847. }
  4848. }
  4849. }