SolidWorks 使い方 入門

ソリッドワークス
メール

3Dプリンターのデータ作成、修正、造形 株式会社 サンエツ

SolidWorks ソリッドワークス 図面の注記を全て英語に自動翻訳

図面の注記を全て翻訳(その2)
プログラムの動作をもう一度動画で確認します。
前のページではclass SolidWorksMacroのコードを作成しました。 このページではclass Form1のコードを作成します。

注記を翻訳(その2)

ヤフーの翻訳サイトを使用しますので、idやphpファイルが変更されてしまうと動作しません。 ご了承ください。

フォームを追加してwebBrowserコントロールを貼り付けてください。その後以下のコードを書いていきます。

  1. ヤフーの翻訳サイトのURLを変数に格納
    //ヤフー翻訳のURL 
     
    const string ur = "http://honyaku.yahoo.co.jp/transtext/";


  2. 注記リストの作成
    List< string> _ant= new List< string>();


  3. 翻訳結果リストの作成
    List< string> _translated =  new List< string>();


  4. ループカウンターの設定
    int count = 0;


  5. 最初の原文をセットして送信
    webBrowser1.Document.GetElementById("textText").InnerText = _ant[count];
    webBrowser1.Document.GetElementById("btn").InvokeMember("Click");


  6. 翻訳が返ってきたときの処理"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(); 
          } 
        }


  7. class SolidWorksMacroに値を渡すtranslated読み取り専用プロパティの作成
    public List <string > translated 
       { 
           
    get  return _translated; } 
       }


  8. 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; } 
       } 
         } 
     }


今回はヤフー翻訳の機能を利用して図面の注記を自動翻訳しました。 自動翻訳だけでも少々勉強になると思います。 時間のある方はいろいろな方法を試してみてください。 尚ヤフー翻訳はIDやPHPファイルを頻繁に変更しますのでBing君の方が安定しているかもしれません。 こちらも時間のある方は比較してみてください。
前のページに戻る場合はコチラ↓
基本操作編INDEXに戻る場合はコチラ↓
LINEで送る