Friday, February 19, 2016

Basic ITK(Teamcenter) Programs

Create Items Sample ITK Program



#include<stdio.h>
#include<tccore/tctype.h>
#include<tccore/item.h>
#include<tccore/aom_prop.h>

int ITK_user_main(int argc, char* argv[])
{

ITK_auto_login();
tag_t item_type_tag = NULLTAG;
tag_t itemRev_type_tag = NULLTAG;
tag_t item_create_input_tag = NULLTAG;
tag_t itemRev_create_input_tag = NULLTAG;
tag_t item_tag = NULLTAG;
tag_t itemRev_tag = NULLTAG;

char* next_id = NULL;

const char *object_name[] = { "TestItemName" };
const char *object_desc[] = { "TestItem Description" };


TCTYPE_find_type("Item",  "Item", &item_type_tag);
TCTYPE_find_type("ItemRevision","ItemRevision", &itemRev_type_tag);

TCTYPE_construct_create_input (item_type_tag, &item_create_input_tag);
TCTYPE_set_create_display_value(item_create_input_tag, "object_name", 1, object_name);
TCTYPE_set_create_display_value(item_create_input_tag, "object_desc", 1, object_desc);

TCTYPE_create_object(item_create_input_tag, &item_tag );
ITEM_save_item(item_tag);

TCTYPE_construct_create_input(itemRev_type_tag,&itemRev_create_input_tag);
 
TCTYPE_set_create_display_value(itemRev_create_input_tag, "object_name", 1, object_name);

AOM_set_value_tag(itemRev_create_input_tag, "items_tag", item_tag);

TCTYPE_create_object(itemRev_create_input_tag, &itemRev_tag);
ITEM_save_rev(itemRev_tag);

ITEM_save_item(item_tag);

return 0;
}

========================================================================

Function which will print a error message with the help of iFail received:


#include <stdlib.h>
#include <tc/tc.h>
#include <sa/tcfile.h>
#include <tccore/item.h>
#include <tc/folder.h>
#include <tccore/workspaceobject.h>
#include <ae/ae.h>

#define ITK_CALL(x) {           \
    int stat;                     \
    char *err_string;             \
    if( (stat = (x)) != ITK_ok)   \
    {                             \
    EMH_get_error_string (NULLTAG, stat, &err_string);                 \
    printf ("ERROR: %d ERROR MSG: %s.\n", stat, err_string);           \
    printf ("FUNCTION: %s\nFILE: %s LINE: %d\n",#x, __FILE__, __LINE__); \
    if(err_string) MEM_free(err_string);                                \
    exit (EXIT_FAILURE);                                                   \
    }                                                                    \
}

Usage:

ITK_CALL( ITEM_create_item( item_id, item_name, type_name, rev_id, new_item, new_rev));

========================================================================

Creating Item and attaching Dataset


#include <stdlib.h>
#include <tc/tc.h>
#include <sa/tcfile.h>
#include <tccore/item.h>
#include <tc/folder.h>
#include <tccore/workspaceobject.h>
#include <ae/ae.h>

#define EXIT_FAILURE 1

static void create_item (char *item_id, tag_t *new_item, tag_t *new_rev)
{
    char item_name[ITEM_name_size_c + 1],  type_name[ITEM_type_size_c + 1] = {""}, rev_id[ITEM_id_size_c + 1] = "A";
       
    strcpy(item_name, item_id);
    printf("Creating Item ID: %s\n", item_id);
    ITEM_create_item( item_id, item_name, type_name, rev_id, new_item, new_rev);

    printf("Saving Item ID: %s\n", item_id);
    ITEM_save_item(*new_item);

    printf("Attaching Item ID: %s to Newstuff Folder\n", item_id);
    FL_user_update_newstuff_folder(*new_item);
}

static void create_dataset(char *type_name, char *name, tag_t item, tag_t rev, tag_t *dataset)
{
    char format_name[AE_io_format_size_c + 1] = "BINARY_REF";
    tag_t  datasettype,  tool;
   
    AE_find_datasettype(type_name, &datasettype);
    if (datasettype == NULLTAG)
    {
        printf("Dataset Type %s not found!\n", type_name);
        exit (EXIT_FAILURE);
    }
   
    AE_ask_datasettype_def_tool(datasettype, &tool);
   
    printf("Creating Dataset: %s\n", name);
    AE_create_dataset(datasettype, name, "", dataset);
   
    AE_set_dataset_tool(*dataset, tool));
    if (strcmp(type_name, "Text")) strcpy(format_name, "TEXT_REF");
   
    AE_set_dataset_format(*dataset, format_name);
    printf("Saving Dataset: %s\n", name);
    AOM_save(*dataset);
   
    /*attach dataset to item revision */
    ITEM_attach_rev_object(rev, *dataset, ITEM_specification_atth);
    ITEM_save_item(item);

}

static void do_it(void)
{
    char   item_id[ITEM_name_size_c + 1], dataset_name[WSO_name_size_c +1], datasettype_name[AE_datasettype_name_size_c +1];
    tag_t  new_item, new_rev, dataset;
         
    printf("Enter new Item ID: ");
    gets(item_id);
   
    printf("Enter Dataset Type (e.g. UGMASTER, UGPART, Text): ");
    gets(datasettype_name);
   
    strcpy(dataset_name, item_id);
    strcat(dataset_name, "/A");  
 
    create_item(item_id, &new_item, &new_rev);
    create_dataset(datasettype_name, dataset_name, new_item, new_rev, &dataset);

    AOM_unload(dataset);
    AOM_unload(new_rev);
    AOM_unload(new_item);
}


int ITK_user_main(int argc, char* argv[])
{
    int status = 0;
    char *message;
   
    ITK_initialize_text_services( 0 );
    status = ITK_auto_login();
 
    if (  (status != ITK_ok))
{
printf("iMAN login NOT successful.\n");
}
    else
    {
        printf("iMAN login successful.\n");
        ITK_set_journalling(TRUE);
        do_it();
    }

    ITK_exit_module(TRUE);
    return status;
}
========================================================================


Saturday, February 13, 2016

How to download videos without any software.

How to download videos without any software.

You will need to follow below steps to download any video from Internet. You just need a browser with which you can find source of any web page(you should see developers window when you press F12 in Chrome).

This takes little patient till understanding, once you understand the process its all easy, so lets start:

1. Open the web page where you are seeing video after all adds finished showing. As somtimes actual video is behind some adds page, so when you click some adds comes up on browser. 
2. Then press F12 in Google Chrome browser a developers window will show up.
3. Press Ctrl+Shift+C, basically i want you to click on upper left most button on developers view.
4. then click on Video, then if any add comes up, then you should close that adds and again press Ctrl+shift+c. When you click on that video, you will see below in Developers windows as shown below:


Here you can see i blacked out some content which is specific for video that i am downloading.

5. Select the src property value in this html page, you can double click on src property value which i have blacked out. Copy the content and paste in new window/tab. You should see the video without any adds.

6. Now just save that HTML page, it will be saved as MP4 file. Save that file to local directory thats it, Very Simple!

7. If you want to download the file using command then you need to follow some more steps given on below page:



Visual script to Download a file from internet.

Visual script to Download a file from internet.


There are so many ways to Download file from internet, there may be scenario arise when you will need files to be downloaded by command.

You need to follow below steps to download file from internet through command.

1. Create a file in local drive with below content, you should give file url in strFileURL variable.

' give complete url of file and local location of file where we are storing the file
    strFileURL = "https://server_name/exact_file_url"
    strHDLocation = "one.avi"

' Fetch the file
    Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")

    objXMLHTTP.open "GET", strFileURL, false
    objXMLHTTP.send()

If objXMLHTTP.Status = 200 Then
Set objADOStream = CreateObject("ADODB.Stream")
objADOStream.Open
objADOStream.Type = 1 'adTypeBinary

objADOStream.Write objXMLHTTP.ResponseBody
objADOStream.Position = 0    'Set the stream position to the start

Set objFSO = Createobject("Scripting.FileSystemObject")
If objFSO.Fileexists(strHDLocation) Then objFSO.DeleteFile strHDLocation
Set objFSO = Nothing

objADOStream.SaveToFile strHDLocation
objADOStream.Close
Set objADOStream = Nothing
End if

Set objXMLHTTP = Nothing

2. and run the below command.

'VScript file_name.vbs

Sunday, February 7, 2016

Useful Window's Commands

I came across below commands which I use regularly, I collected these commands from various blogs and forums and I think you should know below commands

1. Shutdown command:

You can run shutdown the system with below parameters:
  • shutdown -m <machine_name>— shutsdown the remote machine, you can use below parameters with above command
  • -r — restarts
  • shnutdow -s — shutsdown
  • shutdown -l — logoff
  • shutdown -t xx — where xx is number of seconds to wait till shutdown/restart/logoff
  • shutdown -i — gives you a dialog box to fill in what function you want to use
  • shutdown -a — aborts the previous shutdown command
  • shutdown -h — hibernate not for help
  • shutdown -h — hibernate not for help

Additional options:
  • -f — force the selected action
I used to run "shutdown -t 0 -r -f" to forcefully restart the system.

========================================================================


2. To kill process:


TaskKill /F /IM javaw.exe

you can keep any process name instead of javaw.exe, here /F is used for forcefully killing process and IM specifies the image name of the process to be terminated. Use the wildcard (*) to specify all image names. other parameters are below:

/s Computer : Specifies the name or IP address of a remote computer (do not use backslashes). The default is the local computer.

/u Domain \ User : Runs the command with the account permissions of the user specified by User or Domain\User. The default is the permissions of the current logged on user on the computer issuing the command.

/p Password : Specifies the password of the user account that is specified in the /u parameter.

/fi FilterName : Specifies the types of process(es) to include in or exclude from termination. The following are valid filter names, operators, and values.

========================================================================

3. How to install JDK/JRE using command:

Start /wait C:\Kits\jdk-8u73-windows-x64.exe /s INSTALLDIR=C:\apps\Java\jdk-8u73 STATIC=1 WEB_JAVA=0 WEB_JAVA_SECURITY_LEVEL=M SPONSORS=0

=======================================================================

4. Setting environment variable using command:

SETX JAVA_HOME C:\apps\Java\jdk-8u73 /m

========================================================================

5. Go to directory in command prompt:


call cd/d C:\Kits

========================================================================

6. How to get list of files and folders in direcory:

dir /b > filelist.txt 

this will create a file in current directory named filelist.txt

========================================================================

7. How to list all the environment variables:


set > variables.txt

this will list all the variables in variables.txt file.

========================================================================

8. How to map a remote location to a new drive:


Create a bat file("MappingLocation.bat") with below content:

@echo off
if exist Y:\ (
    net use Y: /delete
)
net use Y: \\your\any\remote\location password /user:a_username /persistent:yes

and run that bat file with administrator privilege.

========================================================================


Thursday, February 4, 2016

Powershell Programming

I am just putting togetther simple things that I come to know about powershell and i just said wow!


1. How to Merge XML files using powershell:


Let consider we have two below xml files to merge:

C:/MainFile.xml
<?xml version="1.0"?>
<catalog>
   <Recipe id="rc501">
      <author>Suhas, Tiwaskar</author>
      <title>C++ Cofta</title>
      <price>98.95</price>
      <originated_from>Sewagram</originated_from>
   </Recipe>
   <Recipe id="rc502">
      <author>Harish, Datey</author>
      <title>Java Malai</title>
      <price>1765.00</price>
      <originated_from>Khandesh</originated_from>
   </Recipe>
</catalog>

C:/Delta.xml
<?xml version="1.0"?>
<catalog>
   <Recipe id="rc503">
      <author>Vaibhav, Wakodikar</author>
      <title>.Net Rajma</title>
      <price>98.95</price>
      <originated_from>Karve, Nagar</originated_from>
   </Recipe>
   <Recipe id="rc504">
      <author>Amit, Nandurikar</author>
      <title>PHP Rassam</title>
      <price>116.00</price>
      <originated_from>Aurangabad</originated_from>
   </Recipe>

</catalog>

Now create one more File with name mergeXml.ps1 in C:\  with below content:

[xml]$File1 = Get-Content C:\MainFile.xml
[xml]$File2 = Get-Content C:\Delta.xml
ForEach ($XmlNode in $File2.DocumentElement.ChildNodes){$File1.DocumentElement.AppendChild($File1.ImportNode($XmlNode, $true))}
$File1.Save('C:\Merged.xml')


now on powershell window run this script by running "powershell C:\mergeXML.ps1" it will merge two xml files.


you may get error "The input document has exceeded a limit set by MaxCharactersFromEntities". you need to make one trick so that validator will turn off. Upper code will changed to:

$MainFile = New-Object System.Xml.XmlDocument
$MainFile.XmlResolver = $null
$MainFile.Load($args[0])

$DeltaFile = New-Object System.Xml.XmlDocument
$DeltaFile.XmlResolver = $null
$DeltaFile.Load($args[1])


ForEach ($XmlNode in $DeltaFile.DocumentElement.ChildNodes){$MainFile.DocumentElement.AppendChild($MainFile.ImportNode($XmlNode, $true))}
$MainFile.Save($args[0])

========================================================================


2. Get the output of command in variable using Powershell

$output = cmd /c "set JAVA_HOME"

this will push output of command "set JAVA_HOME" to $output variable, after which you can print that variable content using:

return $output

========================================================================


3. Splitting the content using Powershell

$output = cmd /c "set"

this will just push output of command set to variable $output, if we want to split the content by new line character

ForEach($line in $output){     //looping on lines
    $split = $line -split "="  //split by "="
    $varName = $split[0]
    Write-Host $varName        //writing to console
}


========================================================================

4. How to know if variable is empty or not

There are multiple ways but I am keeping here the simplest one:

if($yourName) {           
Write-Host $yourName" is awesome!"      
}


========================================================================

5. Error: “execution of scripts is disabled on this system”

Yes you need to set execution policy to powershell command prompt like below:

powershell.exe -ExecutionPolicy Unrestricted C:\SampleScript.ps1

this will bypass the restrictions and execute the command.

Documentation
========================================================================

6. Downloading the Amazon S3 Bucket content

# Your account access key - must have read access to your S3 Bucket
$accessKey = "your_access_key"
# Your account secret access key
$secretKey = "your_success_key"
# The region associated with your bucket e.g. eu-west-1, us-east-1, us-west-2, eu-west-1, eu-central-1, ap-southeast-1, ap-northeast-1, ap-southeast-2, ap-northeast-2, sa-east-1. Documentation 
$region = "us-east-1"
# The name of your S3 Bucket
$bucket = "your_s3bucket_name"
# The folder in your bucket to copy, including trailing slash. Leave blank to copy the entire bucket
$keyPrefix = "folder_name/"
# The local file path where files should be copied
$localPath = "D:\YourLocalFolder\"

$objects = Get-S3Object -BucketName $bucket -KeyPrefix $keyPrefix -AccessKey $accessKey -SecretKey $secretKey -Region $region

foreach($object in $objects) {
$localFileName = $object.Key -replace $keyPrefix, ''
if ($localFileName -ne '') {
$localFilePath = Join-Path $localPath $localFileName
Copy-S3Object -BucketName $bucket -Key $object.Key -LocalFile $localFilePath -AccessKey $accessKey -SecretKey $secretKey -Region $region
}
}

====================================================================

7. Creating directory using powershell script


New-Item -ItemType directory -Path C:\Scripts\newDir

====================================================================

8. Adding Content at the end of File


Add-Content C:\Windows\System32\drivers\etc\hosts "`n127.0.0.0 hostname"

====================================================================