Skip to content

Commit d14d421

Browse files
committed
* trying to script
copy/paste * screenshot by script
1 parent 5b55da8 commit d14d421

11 files changed

+107
-26
lines changed

src/AasxIntegrationBase/AasxMenu.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -672,8 +672,10 @@ public IEnumerable<T> FindAll<T>(Func<T, bool> pred = null)
672672

673673
public AasxMenuItemBase FindName(string name)
674674
{
675-
return FindAll((i) => i?.Name?.Trim().ToLower() == name?.Trim().ToLower())
676-
.FirstOrDefault();
675+
return FindAll((i) =>
676+
{
677+
return i?.Name?.Trim().ToLower() == name?.Trim().ToLower();
678+
}).FirstOrDefault();
677679
}
678680

679681
//

src/AasxPackageExplorer/MainWindow.xaml.cs

+45-18
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ This source code may use other Open Source software components (see LICENSE.txt)
2121
using Extensions;
2222
using Microsoft.Win32;
2323
using Newtonsoft.Json;
24+
using NPOI.HPSF;
25+
using Org.BouncyCastle.Asn1.X509;
2426
using System;
2527
using System.Collections.Generic;
2628
using System.ComponentModel;
@@ -2783,27 +2785,52 @@ private void ButtonReport_Click(object sender, RoutedEventArgs e)
27832785
}
27842786
}
27852787

2786-
//private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
2787-
//{
2788-
// // decode
2789-
// var ruic = e?.Command as RoutedUICommand;
2790-
// if (ruic == null)
2791-
// return;
2792-
// var cmd = ruic.Text?.Trim().ToLower();
2788+
/// <summary>
2789+
/// Take a screenshot and save to file
2790+
/// </summary>
2791+
public void SaveScreenshot(string filename = "noname")
2792+
{
2793+
// use the whole Window
2794+
var target = this;
2795+
if (target == null || string.IsNullOrEmpty(filename))
2796+
{
2797+
return;
2798+
}
27932799

2794-
// // see: MainWindow.CommandBindings.cs
2795-
// try
2796-
// {
2797-
// this.CommandBinding_GeneralDispatch(cmd);
2798-
// }
2799-
// catch (Exception err)
2800-
// {
2801-
// throw new InvalidOperationException(
2802-
// $"Failed to execute the command {cmd}: {err}");
2803-
// }
2800+
// prep filename
2801+
if (System.IO.Path.GetExtension(filename) == "")
2802+
filename += ".png";
28042803

2805-
//}
2804+
// needs to be the main thread
2805+
ProgressBarInfo.Dispatcher.BeginInvoke(
2806+
System.Windows.Threading.DispatcherPriority.Background,
2807+
new Action(() =>
2808+
{
2809+
// see: https://stackoverflow.com/questions/5124825/generating-a-screenshot-of-a-wpf-window
2810+
Rect bounds = VisualTreeHelper.GetDescendantBounds(target);
2811+
2812+
RenderTargetBitmap renderTarget = new RenderTargetBitmap((Int32)bounds.Width, (Int32)bounds.Height, 96, 96, PixelFormats.Pbgra32);
28062813

2814+
DrawingVisual visual = new DrawingVisual();
2815+
2816+
using (DrawingContext context = visual.RenderOpen())
2817+
{
2818+
VisualBrush visualBrush = new VisualBrush(target);
2819+
context.DrawRectangle(visualBrush, null, new Rect(new Point(), bounds.Size));
2820+
}
2821+
2822+
renderTarget.Render(visual);
2823+
PngBitmapEncoder bitmapEncoder = new PngBitmapEncoder();
2824+
bitmapEncoder.Frames.Add(BitmapFrame.Create(renderTarget));
2825+
using (Stream stm = System.IO.File.Create(filename))
2826+
{
2827+
bitmapEncoder.Save(stm);
2828+
}
2829+
2830+
// Log
2831+
Log.Singleton.Info("Screenshot saved to {0}.", filename);
2832+
}));
2833+
}
28072834

28082835
private void DisplayElements_SelectedItemChanged(object sender, EventArgs e)
28092836
{

src/AasxPackageExplorer/debug.MIHO.script

+5-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
// Tool("sammaspectimport", "File", "C:\\HOMI\\Develop\\Aasx\\repo\\samm-test\\Batch-MM-2_0_0.ttl");
1111
Tool("editkey");
1212
Select("Submodel", "First");
13+
Select("SME", "First");
14+
Tool("aas-elem-copy");
1315
// Tool("sammaspectimport", "File", "C:\\HOMI\\Develop\\Aasx\\repo\\samm-test\\Aspect_Example_SML_MLP.ttl");
1416
// Tool("exportpredefineconcepts", "File", "C:\HOMI\Develop\Aasx\repo\aid\new.txt");
1517
// Tool("submodelinstancefromsmtconcepts");
@@ -25,5 +27,6 @@ Select("Submodel", "First");
2527
// Tool("WinMaximize");
2628
// Tool("ExportTable", "File", "C:\\HOMI\\Develop\\Aasx\\repo\\new.adoc", "Preset", "AsciiDoc");
2729
// Tool("ToolsFindText", "FindText", "A");
28-
Tool("ToolsReplaceText", "FindText", "A", "ReplaceText", "B", "Do", "all");
29-
// WriteLine(GetLastLogLine());
30+
// Tool("ToolsReplaceText", "FindText", "A", "ReplaceText", "B", "Do", "all");
31+
// WriteLine(GetLastLogLine());
32+
// TakeScreenShot("Test123");

src/AasxPackageExplorer/options-debug.MIHO.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
// "AasxToLoad": "C:\\Users\\homi0002\\Desktop\\SMT_ProductChangeNotification\\IDTA_02036-1-0_SMT_ProductChangeNotification_Draft_v22.aasx",
4141
// "AasxToLoad": "C:\\Users\\homi0002\\Desktop\\SMT_ProductChangeNotification\\IDTA_02036-1-0_SMT_ProductChangeNotification_Examples_v08.aasx",
4242
// "AasxToLoad": "C:\\Users\\homi0002\\Desktop\\SMT_ProductChangeNotification\\SMT_ProductChangeNotification_Draft_v20_spiel.aasx",
43-
"AasxToLoad": "C:\\HOMI\\Develop\\Aasx\\aaspe-testing\\200_Find_Replace\\Sample_SMT.aasx",
43+
"AasxToLoad": "C:\\HOMI\\Develop\\Aasx\\aaspe-testing\\210_Copy_Paste\\Sample_AAS.aasx",
44+
// "AasxToLoad": "C:\\HOMI\\Develop\\Aasx\\aaspe-testing\\200_Find_Replace\\Sample_SMT.aasx",
4445
// "AasxToLoad": "C:\\Users\\homi0002\\Desktop\\SMT_ProductChangeNotification\\IDTA_02036-1-0_SMT_ProductChangeNotification_Examples_v08.aasx",
4546
// "AasxToLoad": "C:\\Users\\homi0002\\Desktop\\tmp\\Syn2tecMachine_P2518_AAS__V3_DL2.aasx",
4647
// "AasxToLoad": "C:\\MIHO\\Develop\\Aasx\\repo\\Syn2tecMachine_P2518_AAS__V3_DL2.aasx",

src/AasxPackageLogic/AasxScript.cs

+24
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public interface IAasxScriptRemoteInterface
3535
Aas.IReferable Select(object[] args);
3636
Aas.IReferable[] SelectAll(object[] args);
3737
Task<bool> Location(object[] args);
38+
void TakeScreenshot(string filename = "noname");
3839
}
3940

4041
public class AasxScript
@@ -207,6 +208,28 @@ public override object Invoke(IScriptContext context, object[] args)
207208
}
208209
}
209210

211+
public class Script_TakeScreenShot : ScriptInvokableBase
212+
{
213+
public Script_TakeScreenShot(AasxScript script) : base(script)
214+
{
215+
script?.AddHelpInfo("TakeScreenShot",
216+
"Takes a screenshot of the main window.",
217+
args: new AasxMenuListOfArgDefs()
218+
.Add("<file>", "Filename to save screenshot to."));
219+
}
220+
221+
public override object Invoke(IScriptContext context, object[] args)
222+
{
223+
string fn = "noname";
224+
if (args != null && args.Length == 1 && args[0] is string afn)
225+
fn = afn;
226+
_script.Remote?.TakeScreenshot(fn);
227+
228+
// done
229+
return 0;
230+
}
231+
}
232+
210233
public class Script_Tool : ScriptInvokableBase
211234
{
212235
public Script_Tool(AasxScript script) : base(script)
@@ -561,6 +584,7 @@ public void StartEnginBackground(
561584
s.Context.Scope.SetItem("FileReadAll", new Script_FileReadAll(this));
562585
s.Context.Scope.SetItem("FileExists", new Script_FileExists(this));
563586
s.Context.Scope.SetItem("GetLastLogLine", new Script_GetLastLogLine(this));
587+
s.Context.Scope.SetItem("TakeScreenShot", new Script_TakeScreenShot(this));
564588
s.Context.Scope.SetItem("Select", new Script_Select(this));
565589
s.Context.Scope.SetItem("SelectAll", new Script_SelectAll(this));
566590
s.Context.Scope.SetItem("Location", new Script_Location(this));

src/AasxPackageLogic/DispEditHelperBasics.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ public void AddActionPanel(
705705

706706
tmi.ActionAsync = async (name, item, ticket) =>
707707
{
708-
if (ticket != null)
708+
if (ticket != null && ticketActionAsync != null)
709709
ticket.UiLambdaAction = await ticketActionAsync(currentI, ticket);
710710
};
711711

src/AasxPackageLogic/DispEditHelperCopyPaste.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ public void DispSmeCutCopyPasteHelper(
493493
// use an action
494494
this.AddActionPanel(
495495
stack, label,
496-
repo: repo,
496+
repo: repo, superMenu: superMenu,
497497
ticketMenu: new AasxMenu()
498498
.AddAction("aas-elem-cut", "Cut",
499499
"Removes the currently selected element and places it in the paste buffer.",

src/AasxPackageLogic/IMainWindow.cs

+5
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ public interface IMainWindow
5353
/// </summary>
5454
void LogShow();
5555

56+
/// <summary>
57+
/// Take a screenshot and save to file
58+
/// </summary>
59+
void SaveScreenshot(string filename = "noname");
60+
5661
/// <summary>
5762
/// Make sure the file repo is visible
5863
/// </summary>

src/AasxPackageLogic/MainWindowScripting.cs

+9
Original file line numberDiff line numberDiff line change
@@ -521,5 +521,14 @@ await CommandBinding_GeneralDispatchAnyUiDialogs(
521521
return true;
522522
}
523523

524+
/// <summary>
525+
/// Take a screenshot and save to file
526+
/// </summary>
527+
public void TakeScreenshot(string filename = "noname")
528+
{
529+
// just pass on
530+
MainWindow?.SaveScreenshot(filename);
531+
}
532+
524533
}
525534
}

src/BlazorExplorer/Data/BlazorSession.MainWindow.cs

+8
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ public async void LogShow()
9191
await DisplayContext.StartFlyoverModalAsync(uc);
9292
}
9393

94+
/// <summary>
95+
/// Take a screenshot and save to file
96+
/// </summary>
97+
public void SaveScreenshot(string filename = "noname")
98+
{
99+
// no such capability
100+
}
101+
94102
public void CommandExecution_RedrawAll()
95103
{
96104
// redraw everything

src/Notes_V3.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -481,4 +481,6 @@ These projects are not migrated or integrated, yet:
481481
* plotting of historical values does not work!! (not sure, if was working)
482482
* aaspe/testing/551_MTP -- working with new OPC UA client
483483
* aaspe/testing/552_OpcUaReadSubmodel -- working
484-
* aaspe/testing/120_Export_AsciiDoc -- working
484+
* aaspe/testing/120_Export_AsciiDoc -- working
485+
* copy/paste of SME direct / via Windows clipboard -- working
486+
*

0 commit comments

Comments
 (0)