3Dプリンターのデータ作成、修正、造形 株式会社 サンエツ
SolidWorks ソリッドワークス 図面の注記を全て英語に自動翻訳
図面の注記を全て翻訳(その2)
プログラムの動作をもう一度動画で確認します。
class Form1
前のページではclass SolidWorksMacroのコードを作成しました。
このページではclass Form1のコードを作成します。
注記を翻訳(その2)
ヤフーの翻訳サイトを使用しますので、idやphpファイルが変更されてしまうと動作しません。
ご了承ください。
フォームを追加してwebBrowserコントロールを貼り付けてください。その後以下のコードを書いていきます。
-
ヤフーの翻訳サイトのURLを変数に格納
//ヤフー翻訳のURL
const string ur = "http://honyaku.yahoo.co.jp/transtext/";
-
注記リストの作成
List<
string> _ant=
new List<
string>();
-
翻訳結果リストの作成
List<
string> _translated =
new List<
string>();
-
ループカウンターの設定
int count = 0;
-
最初の原文をセットして送信
webBrowser1.Document.GetElementById("textText").InnerText = _ant[count];
webBrowser1.Document.GetElementById("btn").InvokeMember("Click");
-
翻訳が返ってきたときの処理"http://honyaku.yahoo.co.jp/darla/php/fc.php"が返ってくる
if (e.Url.ToString().Contains("http://honyaku.yahoo.co.jp/darla/php/fc.php"))
{
count++;
_translated.Add(webBrowser1.Document.GetElementById("trn_textText").InnerText);
// 次の原文をセットして送信
if
(count < _ant.Count)
{
//動画を見やすいようにSystem.Threading.Thread.Sleep(1000);を挿入
System.Threading.
Thread.Sleep(1000);
webBrowser1.Document.GetElementById("textText").InnerText = _ant[count];
webBrowser1.Document.GetElementById("btn").InvokeMember("Click");
}
else
{
this.Close();
}
}
-
class SolidWorksMacroに値を渡すtranslated読み取り専用プロパティの作成
public List
<string
> translated
{
get
{
return _translated; }
}
-
class Form1のコード全文
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace translation.csproj
{
public partial class
Form1 :
Form
{
public
Form1()
{
InitializeComponent();
}
//ヤフー翻訳のURL
const string ur = "http://honyaku.yahoo.co.jp/transtext/";
//注記のリスト
List
<string
> _ant=new
List<string
>();
//class SolidWorksMacroから値を受け取る
public void
antion(List
<string
> ant)
{
_ant = ant;
}
private void
Form1_Shown(object
sender, EventArgs
e)
{
//スクリプトエラーを出さない
webBrowser1.ScriptErrorsSuppressed =
true;
webBrowser1.Navigate(ur);
}
//翻訳結果のリスト
List<
string> _translated =
new List
<string
>();
//ループカウンター
int count = 0;
private void
webBrowser1_DocumentCompleted(
object sender,
WebBrowserDocumentCompletedEventArgs
e)
{
if
(ur == e.Url.ToString())
{
if
(count < _ant.Count)
{
// 最初の原文をセットして送信
webBrowser1.Document.GetElementById("textText").InnerText = _ant[count];
webBrowser1.Document.GetElementById("btn").InvokeMember("Click");
}
}
// 翻訳が返ってきたときの処理
if
(e.Url.ToString().Contains("http://honyaku.yahoo.co.jp/darla/php/fc.php"))
{
count++;
_translated.Add(webBrowser1.Document.GetElementById("trn_textText").InnerText);
// 次の原文をセットして送信
if
(count < _ant.Count)
{
//動画を見やすいようにSystem.Threading.Thread.Sleep(1000);を挿入
System.Threading.Thread
.Sleep(1000);
webBrowser1.Document.GetElementById("textText").InnerText = _ant[count];
webBrowser1.Document.GetElementById("btn").InvokeMember("Click");
}
else
{
this.Close();
}
}
}
//translated読み取り専用プロパティ
public List
<string
> translated
{
get
{ return
_translated; }
}
}
}